-
用于编写函数以获取链表中第 n 个节点的 C#程序
用于编写函数以获取链表中第 n 个节点的 C# 程序
原文:https://www . geesforgeks . org/c-写程序-函数-获取链表中的第 n 个节点-2/
编写一个 GetNth()函数,该函数接受一个链表和一个整数索引,并返回存储在该索引位置的节点中的数据值。
例:
Input: 1->10->30->14, index = 2
Output: 30
The node at index 2 is 30
算法:
1\. Initialize count = 0
2\. Loop through the link list
a. If count is equal to the passed index then return current
node
b. Increment count
c. Change current to point to next of the current.
实施:
C
// C# program to find n'th node
// in linked list
using System;
using System.Diagnostics;
public class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
class LinkedList
{
// Head of list
Node head;
// Takes index as argument and
// return data at index
public int GetNth(int index)
{
Node current = head;
// Index of Node we are
// currently looking at
int count = 0;
while (current != null)
{
if (count == index)
return current.data;
count++;
current = current.next;
}
/* If we get to this line, the caller
was asking for a non-existent element
so we assert fail */
Debug.Assert(false);
return 0;
}
/* Given a reference to the head of a list
and an int, inserts a new Node on the
front of the list. */
public void push(int new_data)
{
// 1\. Alloc the Node and put data
Node new_Node = new Node(new_data);
// 2\. Make next of new Node as head
new_Node.next = head;
// 3\. Move the head to point to new Node
head = new_Node;
}
// Driver code
public static void Main(String[] args)
{
// Start with empty list
LinkedList llist = new LinkedList();
// Use push() to construct list
// 1->12->1->4->1
llist.push(1);
llist.push(4);
llist.push(1);
llist.push(12);
llist.push(1);
// Check the count function
Console.WriteLine("Element at index 3 is " +
llist.GetNth(3));
}
}
// This code is contributed by Arnab Kundu
输出:
Element at index 3 is 4
时间复杂度: O(n)
方法 2-带递归:
这个方法是 MY_DOOM 贡献的。
算法:
getnth(node, n)
1\. Initialize count = 0
2\. if count==n
return node->data
3\. else
return getnth(node->next, n-1)
实现:
c
// C# program to find n'th node in
// linked list using recursion
using System;
class GFG
{
// Link list node
public class Node
{
public int data;
public Node next;
public Node(int data)
{
this.data = data;
}
}
/* Given a reference (pointer to pointer)
to the head of a list and an int, push
a new node on the front of the list. */
static Node push(Node head, int new_data)
{
// Allocate node
Node new_node = new Node(new_data);
// Put in the data
new_node.data = new_data;
new_node.next = head;
head = new_node;
return head;
}
/* Takes head pointer of the linked list
and index as arguments and return data
at index*/
static int GetNth(Node head, int n)
{
// Base Condition
if (head == null)
return -1;
int count = 0;
// If count equal too n return
// node.data
// Test Condition
if (count == n)
return head.data;
// Recursively decrease n and
// increase head to next pointer
return GetNth(head.next, n - 1);
}
// Driver code
public static void Main()
{
// Start with the empty list
Node head = null;
// Use push() to construct list
// 1.12.1.4.1
head = push(head, 1);
head = push(head, 4);
head = push(head, 1);
head = push(head, 12);
head = push(head, 1);
// Check the count function
Console.Write("Element at index 3 is {0}",
GetNth(head, 3));
}
}
// Code improvement by Aishwarya Mittal
// This code contributed by PrinciRaj1992
输出:
Element at index 3 is 4
版权属于:月萌API www.moonapi.com,转载请注明出处
本文链接:https://www.moonapi.com/news/27774.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数