如何在链表中创建多个节点然后遍历节点

时间:2011-09-08 10:12:30

标签: c# data-structures linked-list

我正在学习如何在c#中创建一个链表。我有以下代码,不适合我。我只想在主体中添加节点,如下所示,然后迭代将打印到控制台的所有节点。

using System;

class node
{
    public object data;
    public node next;

    public node()
    {
        data = null;
        next = null;
    }
    public node(object o)
    {
        data = o;
        next = null;
    }
    public node(object data, node next)
    {
        this.data = data;
        this.next = next;
    }    
}

class linkedList
{
    private node headNode;
    private node tailNode;

    int node_count;

    public void add(object entry)
    {
        if (headNode == null)
        {
            node newNode = new node(entry);
            headNode = newNode;
            ++node_count;
        }
        else
        {
            if (node_count == 1)
            {
                node newNode = new node(entry, headNode);
                tailNode = newNode;                
            }
            else 
            {
                node newNode = new node(entry, tailNode);
                tailNode = newNode;
            }
            ++node_count;
        }
    }

    public void returnData()
    {
        if (headNode.next != null)
        {
            while (headNode.next != null)
            { 
                Console.WriteLine(headNode.data + "\n");
            }
        }
        else
            Console.WriteLine("Not Available");
    }
}

class Exercise
{
    static int Main()
    {
        linkedList ll = new linkedList();
        ll.add(8);
        ll.add(2);
        ll.add(7);
        ll.add(4);
        ll.add(9);
        ll.add(10);

        ll.returnData();

        Console.ReadLine();
        return 0;
    }
}

3 个答案:

答案 0 :(得分:2)

您的代码完全被破坏了。

这是运行它所需的最小变化

using System;

class node
{
    public object data;
    public node next;

    public node()
    {
        data = null;
        next = null;
    }
    public node(object o)
    {
        data = o;
        next = null;
    }
    public node(object data, node next)
    {
        this.data = data;
        this.next = next;
    }    
}

class linkedList
{
    private node headNode;
    private node tailNode;

    int node_count;

    public void add(object entry)
    {
        node newNode = new node(entry);
        if (headNode == null)
            headNode = newNode;
        if (tailNode != null)
            tailNode.next = newNode;
        tailNode = newNode;
        ++node_count;
    }

    public void returnData()
    {
        node currentNode = headNode;

        if (currentNode == null)
            Console.WriteLine("Not Available");

        while (currentNode != null) {
            Console.WriteLine(currentNode.data);
            currentNode = currentNode.next;
        }

    }
}

class Exercise
{
    static int Main()
    {
        linkedList ll = new linkedList();
        ll.add(8);
        ll.add(2);
        ll.add(7);
        ll.add(4);
        ll.add(9);
        ll.add(10);

        ll.returnData();

        return 0;
    }
}

答案 1 :(得分:1)

你有点转身:D

首先,所有类名都应该以uppercap开头,因此Node(不是节点)和LinkedList(不是linkedList)。

现在您可以使用此功能,但如果您尝试返回仅包含一个节点的列表,则无法使用此功能...因为您正在添加节点...

    public void returnData()
    {
        if (tailNode.next != null)
        {
            Node currentNode = tailNode;
            while (currentNode != null)
            { 
                Console.WriteLine(currentNode.data + "\n");
                currentNode = currentNode.next;
            }
        }
        else
            Console.WriteLine("Not Available");
    }

但是如果你想要一个更规则的经典链表,你可以将链表添加功能更改为...

    public void add(object entry)
    {
        if (headNode == null)
        {
            Node newNode = new Node(entry);
            headNode = newNode;
            ++Node_count;
        }
        else
        {
            if (Node_count == 1)
            {
                Node newNode = new Node(entry);
                headNode.next = newNode;
                tailNode = newNode;                
            }
            else 
            {
                Node newNode = new Node(entry);
                tailNode.next = newNode;
                tailNode = newNode;
            }
            ++Node_count;
        }
    }

和returnData到...

    public void returnData()
    {
        if (headNode.next != null)
        {
            Node currentNode = headNode;
            while (currentNode != null)
            { 
                Console.WriteLine(currentNode.data + "\n");
                currentNode = currentNode.next;
            }
        }
        else
            Console.WriteLine("Not Available");
    }

如果要将节点添加到列表的前面,可以修改添加功能,此代码会将节点添加到列表的末尾。

如果你有其他问题,希望这会有所帮助

答案 2 :(得分:0)

要打印您的数据,您需要执行while (headNode.next != null)以便遍历您的列表。

看起来你永远不会更新node.next

if (node_count == 1) // this seems unnecessary.
{
   node newNode = new node(entry, headNode);
   tailNode = newNode; 
   //should be node.next = newnode;                
}