链表如何工作?

时间:2011-08-05 14:32:12

标签: c# data-structures linked-list

我正在阅读一个教程,我也看了一遍谷歌,但我找不到关于链表如何工作的细节的详细解释...我真的很困惑的结构/格式,我真的希望链接list对我来说很有意义,因为它们听起来很棒,是一个可调整和可修改的数组......如果你需要看看我在说什么,下面是我从tut得到的一些代码。我对附加方法或删除方法,它们的作用以及列表工作中的尾部方法有点困惑......这本书只是从一个例子开始,并没有给出解释..

请帮助解决这个困惑..

class ListEntry
{
    int data;
    ListEntry next;

    public ListEntry( int d )
    {
        data = d;
        next = null;
    }

    public int Data
    {
        get{ return data; }
        set{ data = value; }
    }

    public ListEntry Next
    {
        get{ return next; }
        set{ next = value; }
    }

    public override string ToString( )
    {
        return( data.ToString( ) );
    }
}


class TestProgram
    {
        static void Main( )
        {
            List list = new List( );

            list.Append( 3);
            Console.WriteLine( list );

            list.Append( 1 );
            Console.WriteLine( list );

            list.Append( 6 );
            Console.WriteLine( list );

            list.Prepend( 4 );
            Console.WriteLine( list );

            // continued…

// Continued…

        list.Prepend( 5 );
        Console.WriteLine( list );

        list.DeleteFirst( 4 );
        Console.WriteLine( list );

        list.Prepend( 2 );
        Console.WriteLine( list );

        Console.WriteLine( "Head data = " + list.Head );
        Console.WriteLine( "Tail data = " + list.Tail );

        list.Clear( );
        Console.WriteLine( list );

        Console.WriteLine( "IsEmpty = " + list.IsEmpty );
    }
}



using System;

class List
{
    ListEntry head;
    ListEntry tail;

    class ListEntry
    {
        // Put declaration of ListEntry here.  Nesting of the classes is valid.  In fact, class nesting is
        // preferable if one class is only used within the context of another class.
    }

    public List( )
    {
        head = null;
        tail = null;
    }

    // Continued…


public int Head
{
    get{ return head.Data; }
}

public int Tail
{
    get{ return tail.Data; }
}

public bool IsEmpty
{
    get{ return( head == null ); } 
}

public override string ToString( )
{
    string tmp = "";

    ListEntry current = head;
    if( current == null )
    {
        tmp = "Empty";
    }
    while( current != null )
    {
        tmp += current + " ";
        current = current.Next;
    }
    return( tmp );
}

public void Append( int i )
{
    ListEntry tmp = new ListEntry( i );

    tmp.Next = null;

    if( head == null )
    {
        head = tmp;
    }
    else
    {
        tail.Next = tmp;
    }
    tail = tmp;
}

public void Prepend( int i )
{
    ListEntry tmp = new ListEntry( i );

    tmp.Next = head;

    if( head == null )
    {
        tail = tmp;
    }
    head = tmp;
}

public void DeleteFirst( int i )
{
    ListEntry current = head;
    ListEntry previous = null;

    while( current != null && current.Data != i )
    {
        previous = current;
        current = current.Next;
    }
    if( current == null )
    {
        throw new ArgumentException( "List entry not found" );
    }

    // Continued…


// Continued…

    if( current == head )
    {
        head = current.Next;
    }
    else
    {
        previous.Next = current.Next;
    }
    if( current == tail )
    {
        tail = previous;
    }
}

还有其他方法,例如: Sort()方法 FindFirst()方法 FindNext()方法 InsertBefore()方法 InsertAfter()方法

但是现在基本的都很好..

2 个答案:

答案 0 :(得分:4)

链接列表是用于收集一系列对象的数据结构。 “头部”是序列中的第一个项目。 “Tail”是序列中的最后一个对象。链表(节点)中的每个项目都将具有一个名为Next的属性(如果它是双向链接则为Previous),该属性指向列表中的Next或Previous项。这些下一个和前一个项目只指向集合中的下一个或上一个项目,因此要迭代它们,您必须按顺序执行。

想象一下像链中的链接一样的链表。要进入列表中的第5项,您可以从链中的第一个链接开始,然后按照它直到达到第5项。希望这有点帮助。

http://en.wikipedia.org/wiki/Linked_list

答案 1 :(得分:2)

C#(通用)中的简单单链表实现:

public class LinkedList<T>
{
    private Node<T> head;

    public void AddAtFront(T data)
    {
        this.head = new Node<T>(data, this.head);
    }

    public void AddAtBack(T data)
    {
        var node = new Node<T>(data);
        var current = this.head;

        if (current == null)
        {
            this.head = node;
        }
        else
        {
            while (current.Next != null)
            {
                current = current.Next;
            }

            current.Next = node;
        }
    }

    public Node<T> Front
    {
        get
        {
            return this.head;
        }
    }

    public Node<T> Back
    {
        get
        {
            var current = this.head;

            if (current != null)
            {
                while (current.Next != null)
                {
                    current = current.Next;
                }
            }

            return current;
        }
    }

    public Node<T> RemoveAtFront()
    {
        var node = this.head;

        if (node != null)
        {
            this.head = node.Next;
        }

        return node;
    }

    public Node<T> RemoveAtBack()
    {
        var current = this.head;

        if (current != null)
        {
            if (current.Next == null)
            {
                this.head = null;
            }
            else
            {
                Node<T> nextToLast = null;

                while (current.Next != null)
                {
                    nextToLast = current;
                    current = current.Next;
                }

                nextToLast.Next = null;
            }
        }

        return current;
    }
}

public class Node<T>
{
    private readonly T data;

    private Node<T> next;

    public Node(T data)
    {
        this.data = data;
    }

    public Node(T data, Node<T> next)
    {
        this.data = data;
        this.next = next;
    }

    public T Data
    {
        get
        {
            return this.data;
        }
    }

    public Node<T> Next
    {
        get
        {
            return this.next;
        }

        set
        {
            this.next = value;
        }
    }
}