簡體   English   中英

創建一個通用的鏈表類來創建對象鏈

[英]Create a generic linked list class to create a chain of objects

請說明該任務是關於什么的?

“創建一個通用的鏈表類,使我們能夠創建不同類型的鏈對象。”

我們是否需要創建一個類型為linkedlist的類並實現列表接口?

class LinkedList<T>:IList<T>
{
   //implement interface methods here?
}

請舉個例子。

鏈接列表是一個特殊的列表,其中列表中的每個元素(或每個元素的容器對象)都具有對列表中下一項的直接引用(“鏈接”)。 此類列表未使用數組實現。

單鏈接列表通常僅具有指向下一個項目的鏈接,而最后一個項目為null以指示列表的結尾。

一個雙向鏈接的列表具有指向下一個和上一個項目的鏈接,並帶有空值以指示列表的每個結尾。

鏈接列表的優點是插入和刪除都非常快。 遍歷整個列表也具有良好的性能,但是非線性搜索可能很慢。

通常,鏈表的實現應實現IEnumerable<T>接口。 實現IList<T>將促進低效線性搜索的使用。

鏈接列表的.NET實現具有以下聲明(減去一些無關緊要的內容)。

LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable

IList<T>接口一樣,我看不到為什么實現了ICollectionICollection<T>接口,但是它們已經實現了。

元素容器對象(具有鏈接)如下所示:

public sealed class LinkedListNode<T>
{
    public LinkedListNode<T> Next { get; }
    public LinkedListNode<T> Previous { get; }
    public T Value { get; set; }
}

怎么樣?

您需要創建自己的新類的通用鏈表。 這是完整的解決方案。 根據以上評論..希望能對您有所幫助..

class Program
{

    static void Main(string[] args)
    {
       // string linked List
        GenericsLinkedList<string> stringLinkedList = new GenericsLinkedList<string>(); //object 1
        string s1 = "Yes";
        string s2 = "No";
        string s3 = "True";
        string s4 = "False";
        stringLinkedList.AddHead(s1);
        stringLinkedList.AddHead(s2);
        stringLinkedList.AddHead(s3);
        stringLinkedList.AddHead(s4);
        //display List
        foreach (string str in stringLinkedList)
        {
            Console.WriteLine("----"+str);
        }

        //Integer LinkedList
        GenericsLinkedList<int> integerList = new GenericsLinkedList<int>();
        int n1 = 1;
        int n2 = 2;
        int n3 = 3;

        integerList.AddHead(n1);
        integerList.AddHead(n2);
        integerList.AddHead(n3);

        foreach (int Intger in integerList)
        {
            Console.WriteLine("----" + Intger);
        }


        Console.ReadKey();


    }
}


// Generic Linked List
class GenericsLinkedList<T>
{
    class LinkedlistNode
    {
        private LinkedlistNode next;
        private T item;

        public LinkedlistNode(T t)
        {
            next = null;
            item = t;

        }
        public LinkedlistNode Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
        public T Item
        {
            get
            {
                return item;
            }
            set
            {
                item = value;
            }
        }       
    }
   private LinkedlistNode head;
   public GenericsLinkedList()
   {
       head = null;
   }
   public void AddHead(T t)
   {
       LinkedlistNode node = new LinkedlistNode(t);
       node.Next = head;
       head = node;
   }
   public IEnumerator<T> GetEnumerator()
   {
       LinkedlistNode current = head;
       while(current != null)
       {
           yield return current.Item;
           current = current.Next;
       }

   }

}
namespace GenericLinkedList
{

// generic linked list node
public class GenericNode<T>
{
    public T data;
    public GenericNode<T> nextNode = null;

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

// generic linked list
public class GenericLinkedList<T>
{
    private GenericNode<T> head = null;

    public void Add(T newListItem)
    {
        if (head == null)
        {
            head = new GenericNode<T>(newListItem);
        }
        else
        {
            GenericNode<T> curr = head;
            while (curr.nextNode != null)
            {
                curr = curr.nextNode;
            }
            curr.nextNode = new GenericNode<T>(newListItem);
        }
    }

    public void DisplayNodes()
    {
        GenericNode<T> curr = head;
        while (curr != null)
        {
            System.Console.WriteLine(curr.data);
            curr = curr.nextNode;
        }
    }
} 

class TestGenericLinkedList
{
    static void Main(string[] args)
    {
        GenericLinkedList<System.Object> gll = new GenericLinkedList<System.Object>();
        gll.Add(12);
        gll.Add("string");
        gll.Add(false);
        gll.DisplayNodes();
    }
    }
}
}

由於此代碼以某種方式消除了泛型的含義,因此可能很愚蠢,但是我認為它們的含義是這樣。

class Generic<T> 
{
    public  T t;

}
static void Main(string[] args)
{
    Generic<object>[] genericarray = new Generic<object>[3];
    for (int i = 0; i < genericarray.Length; i++)
        genericarray[i] = new Generic<object>();
    int a = 0;
    double b = 0.515151513163;
    string c = "s.dçfslsfn";
    genericarray[0].t = a;
    genericarray[1].t = b;
    genericarray[2].t = c;
}

對於鏈表,我通常不建議實現IList ,因為IList強烈暗示着對列表中任何成員的恆定訪問。 我建議實現ICollection ,然后添加鏈接列表必不可少的其他方法,例如PushFrontPopBack等。您可以查看LinkedList<T>類的MSDN文檔以進行比較( http://msdn.microsoft。 com / en-us / library / he2s3bh7.aspx ),盡管您應該單獨實現類。

這應該做

http://msdn.microsoft.com/en-us/library/0x6a29h6.aspx

// type parameter T in angle brackets

public class GenericList {//嵌套類在T上也是通用的。private類Node {// T在非通用構造函數中使用。 公共節點(T t){next = null; 數據= t; }

    private Node next;
    public Node Next
    {
        get { return next; }
        set { next = value; }
    }

    // T as private member data type.
    private T data;

    // T as return type of property.
    public T Data  
    {
        get { return data; }
        set { data = value; }
    }
}

private Node head;

// constructor
public GenericList() 
{
    head = null;
}

// T as method parameter type:
public void AddHead(T t) 
{
    Node n = new Node(t);
    n.Next = head;
    head = n;
}

public IEnumerator<T> GetEnumerator()
{
    Node current = head;

    while (current != null)
    {
        yield return current.Data;
        current = current.Next;
    }
}

}

具有以下實現的更多功能http://msdn.microsoft.com/zh-cn/library/0x6a29h6.aspx

 public class GenericList<T>
{
   private class Node
    {

        public Node(T t)
        {
            next = null;
            data = t;
        }

        private Node next;
        public Node Next
        {
            get { return next; }
            set { next = value; }
        }


        private T data;


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

    private Node head;
    private Node tail;
    private int count;


    public GenericList()
    {
        head = null;
        tail = null;
        count = 0;
    }


    public void AddHead(T t)
    {
        if (head == null)
            head = tail = new Node(t);
        else
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }
        count++;
    }

    public void AddTail(T t)
    {
        if(tail == null)
        {
            head = tail = new Node(t);
        }
        else
        {
            Node n = new Node(t);
            tail.Next = n;
            tail = n;

        }
        count++;
    }


    public void InsertAt(T t,int index)
    {
        if (index < 0 || index > count)
            throw new ArgumentOutOfRangeException();
        else if (index == 0)
            AddHead(t);
        else if (index == count)
            AddTail(t);
        else
        {
            Node currentNode = head;
            for (int i = 0; i < index - 1; i++)
            {
                currentNode = currentNode.Next;
            }
            Node newNode = new Node(t);
            newNode.Next = currentNode.Next;
            currentNode.Next = newNode;
        }
        count++;
    }


    public void Reverse()
    {
        if (head == null || head.Next == null)
            return;
        tail = head;
       Node previousNode = null;
       Node currentNode = head;
      Node nextNode = head.Next;
        while (currentNode != null)
        {
            currentNode.Next = previousNode;
            if (nextNode == null)
                break;
            previousNode = currentNode;
            currentNode = nextNode;
            nextNode = nextNode.Next;

        }

        head = currentNode;

    }


    public IEnumerator<T> GetEnumerator()
    {
        Node current = head;

        while (current != null)
        {
            yield return current.Data;
            current = current.Next;
        }
    }
}
    static void Main()
    {
        var list = new LinkedList<object>();
        list.AddLast("My string");
        list.AddLast(1.5);
        list.AddLast(2);
        list.AddLast(true);

        var en = list.GetEnumerator();
        while (en.MoveNext())
            Console.WriteLine(en.Current);

        Console.ReadKey();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM