0

Please explain what this task is about?

"Create a generic linked list class that enables us to create a chain objects of different types."

Do we need to create a class of type linkedlist and implement list interface?

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

Please give example.

3
  • can u elaborate "one does not, typically, want to subclass a ready-made example of a different data-structure" please ? Commented Mar 10, 2010 at 23:23
  • Sorry, misread it as List. Actually, I take that back now I've looked at the edit history... Commented Mar 10, 2010 at 23:27
  • Please ensure that your problem description is accurate. Do you mean "can create different instances of the list class, where each instance can hold a single type of element, but where different instances of the list can hold different elements", or "where each instance of the list class can hold different types of elements"? In other words, do you want a strongly typed list or not? (I suspect that you want a strongly typed list, but the text is poorly worded in this regard) Commented May 13, 2010 at 22:09

8 Answers 8

2

A linked list is a special list whereby each element (or per-element container object) in the list has a direct reference (the "link") to the next item in the list. This type of list is not implemented using an array.

A singly-linked list usually only has a link to the next item with the final item being null to indicate the end of the list.

A doubly-linked list has a link to both the next and previous items with nulls to indicate each end of the list.

The advantage with a linked list is that inserts and removals are exceedingly quick. Iterating over the entire list also has good performance, but non-linear searching can be slow.

Typically an implementation of a linked list should implement the IEnumerable<T> interface. Implementing IList<T> would promote the use of inefficient linear searches.

The .NET implementation of a linked list has the following declaration (minus some irrelevant cruft).

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

As with the IList<T> interface I don't see why the ICollection & ICollection<T> interfaces have been implemented, but they have.

The element container object (that has the links) looks like this:

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

How's that?

Sign up to request clarification or add additional context in comments.

Comments

2

You need to create your own new Class of Generic Linked List. here is the complete solution. according to above comment.. Hope it helps..

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;
       }

   }

}

Comments

1

It can be silly since this code somehow eliminates the meaning of generic, however I think they mean this.

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;
}

Comments

1
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();
    }
    }
}
}

Comments

0

For a linked list, I wouldn't typically recommend implementing IList, since IList strongly implies constant-time access to any member of the list. I would recommend implementing ICollection, then adding additional methods that are integral to linked lists, such as PushFront, PopBack, etc. You might look at the MSDN documentation for the LinkedList<T> class for comparison (http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx), though you should implement your class separately.

Comments

0

This should do it

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

// type parameter T in angle brackets

public class GenericList { // The nested class is also generic on T. private class Node { // T used in non-generic constructor. public Node(T t) { next = null; data = 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;
    }
}

}

Comments

0
    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();
    }

Comments

0

More Functionalities with following implementation http://msdn.microsoft.com/en-us/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;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.