[英]How to add nodes to a single linked list in c#
我已經開始學習C#中的算法。 我一直在嘗試自己創建一個鏈表算法(只是為了好玩)。 我有一個問題。 我想知道如何將節點添加到列表中? 現在,我有三種與節點交互的方法,但是列表為空,因此什么也沒有發生。
節點類別
class Node {
public int info;
public Node link;
public Node (int i)
{
info = i;
link = null;
}
}
SingleLinkedList類
class SingleLinkedList {
private Node start;
public SingleLinkedList ()
{
start = null;
}
public void CreateList() {
//TODO: Create the linked list here
}
public void DisplayList() {
Node p;
if (start == null) {
Console.WriteLine ("Your list is empty, idiot");
return;
}
Console.WriteLine ("List is : ");
p = start;
while (p != null) {
Console.WriteLine (p.info + " ");
p = p.link;
}
Console.WriteLine ();
}
public void CountNodes() {
int n = 0;
Node p = start;
while (p != null) {
n++;
p = p.link;
}
Console.WriteLine ("Number of nodes in list is = " + n);
}
public bool Search (int x) {
int position = 1;
Node p = start;
while (p != null) {
if (p.info == x)
break;
position++;
p = p.link;
}
if (p == null) {
Console.WriteLine (x + "not found in list because use an idiot");
} else {
Console.WriteLine (x + "is at position " + position);
return true;
}
return false;
}
}
主要
int choice, data;
SingleLinkedList list = new SingleLinkedList ();
Console.WriteLine ("1.Display List");
Console.WriteLine ("2.Count Nodes");
Console.WriteLine ("search for an integer");
Console.WriteLine ("Enter your choice master: ");
choice = Convert.ToInt32 (Console.ReadLine ());
switch (choice) {
case 1:
list.DisplayList ();
break;
case 2:
list.CountNodes ();
break;
case 3:
Console.WriteLine ("Enter the element to be searched");
data = Convert.ToInt32 (Console.ReadLine ());
list.Search (data);
break;
default:
break;
}
如何在SingleLinkedList類中實現CreateList方法以將節點添加到列表中?
單鏈列表模式維護指向第一個節點的指針/引用,並且每個項目都包含指向列表中下一個節點的指針/引用。 追加到列表意味着找到空引用(無論是根引用還是在鏈的末尾),並用對新節點的引用來填充它:
public void Append(Node value)
{
// check if we are adding to an empty list
if (start == null)
start = value;
else
{
// find the last valid node
Node curr;
for (curr = start; curr.link != null; curr = curr.link);
// add the item
curr.link = value;
}
}
不利的一面是,列表中的項目越多,找到下一個添加下一個項目所需的時間就越長。 換句話說,這是一個O(N)操作,當您一次添加數千個項目時,這一點變得非常重要。 對於不超過一百件的物品,您可能不會注意到太多,但是請嘗試將100,000件物品添加到該列表中。
幸運的是,只需跟蹤列表中的最后一項-尾部,就可以將列表中的Append操作簡化為O(1)足夠簡單。
public class SingleLinkedList
{
Node head;
Node tail;
public void Append(Node value)
{
if (head == null)
head = value;
else
tail.link = value;
tail = value;
}
}
現在,您可以將商品添加到一百萬個商品的列表中,並且速度與列表中商品完全相同(給定或花費幾微秒)。 當您從列表中刪除項目時,您只需要記住更新tail
。
至於搜索等,您可以實現IEnumerable
並使用LINQ來完成所有工作。 或添加為您執行此操作的Items
屬性:
public IEnumerable<int> Items
{
get
{
for (var next = head; next != null; next = next.link)
yield return next.value;
}
}
現在,您可以使用以下命令測試列表中存在的項目:
if (list.Items.Any(i => i == somevalue))
{
}
public void AddNodeToList(Node nodeToBeAdded)
{
Node temp = null;
if (start == null)
{
start = nodeToBeAdded;
}
else
{
temp = start;
while (temp.link != null)
{
temp = temp.link;
}
temp.link = nodeToBeAdded;
}
nodeToBeAdded.link = null;
}
public void CreateList(Node[] nodesToBeAdded)
{
foreach (Node item in nodesToBeAdded)
{
this.AddNodeToList(item);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.