LinkedList错误显示(字符串生成器)

时间:2010-04-05 09:31:04

标签: c#

以下程序是基本链表,分为3类 在测试者类(主要)中,我在列表中添加了几个数字(已排序) 但是我得到了结果,我得到了结果: LinkedList.LinkedList
stringbuilder有问题(该程序首先在java中使用字符串缓冲区,但我认为应该是相同的吗?)

LinkedListTester.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkedList
{
public class LinkedListTester
{
    static void Main(string[] args)
    {
        LinkedList ll = new LinkedList();
        ll.addDataSorted(5);
        ll.addDataSorted(7);
        ll.addDataSorted(13);
        ll.addDataSorted(1);
        ll.addDataSorted(17);
        ll.addDataSorted(8);
        Console.WriteLine(ll);
    }
}

}/*LinkedList*/

LinkedList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkedList
{
public class LinkedList
{
    //toestand
    private LinkedListNode first;
    private LinkedListNode last;

    //gedrag
    public LinkedList()
    {
        first = null;
        last = null;
    }

    public void addDataInFront(int data)
    {
        first = new LinkedListNode(data, first);
        if (last == null){
            last = first;
        }
    }/*addDataInFront*/

    public void addDataToBack(int data)
    {
        if (first == null)
        {
            addDataInFront(data);
        }
        else
        {
            last.setNext(new LinkedListNode(data, null));
            last = last.getNext();
        }
    }/*addDataToBack*/

    public void addDataSorted(int data)
    {
        if (first == null || first.getData() > data)
        {
            addDataInFront(data);
        }
        else
        {
            LinkedListNode currentNode = first;
            while (currentNode.getNext() != null && currentNode.getNext().getData() < data)
            {
                currentNode = currentNode.getNext();
            }

            currentNode.setNext(new LinkedListNode(data, currentNode.getNext()));
            currentNode = currentNode.getNext();

            if (currentNode.getNext() == null)
            {
                last = currentNode;
            }
        }
    }/*addDataSorted*/

    public String toString()
    {
        StringBuilder Buf = new StringBuilder();
        LinkedListNode currentNode = first;
        while (currentNode != null)
        {
            Buf.Append(currentNode.getData());
            Buf.Append(' ');
            currentNode = currentNode.getNext();
        }
        return Buf.ToString();
    }/*toString*/



}/*LinkedList*/

}/*LinkedList*/

一个LinkedListNode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkedList
{
public class LinkedListNode
{
    //toestand
    private int data;
    private LinkedListNode next;
    private LinkedListNode previous;

    //gedrag
    public LinkedListNode(int data, LinkedListNode next)
    {
        this.data = data;
        this.next = next;
        this.previous = null;
    }

    public LinkedListNode(int data, LinkedListNode next, LinkedListNode previous)
    {
        this.data = data;
        this.next = next;
        this.previous = previous;
    }

    public LinkedListNode getNext()
    {
        return next;
    }

    public LinkedListNode getPrevious()
    {
        return previous;
    }

    public void setNext(LinkedListNode next)
    {
        this.next = next;
    }

    public void setPrevious(LinkedListNode previous)
    {
        this.previous = previous;
    }

    public int getData()
    {
        return data;
    }


}/*LinkedListNode*/
}/*LinkedList*/

1 个答案:

答案 0 :(得分:2)

public **override** String **ToString**() 
{ 
    StringBuilder Buf = new StringBuilder(); 
    LinkedListNode currentNode = first; 
    while (currentNode != null) 
    { 
        Buf.Append(currentNode.getData()); 
        Buf.Append(' '); 
        currentNode = currentNode.getNext(); 
    } 
    return Buf.ToString(); 
}/*toString*/