views:

717

answers:

6

Hi folks, 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.

TIA

A: 

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.

JSBangs
+2  A: 

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?

Enigmativity
A: 

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

}

blackweb
+2  A: 

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

   }

}
Mussarat Aziz
A: 

// [email protected]

// Oct 10th, 2010

// MCTS Exam 70-536 Chapter 1 Review - Create a

// linked-list generic class that enables you to

// create chain of ojects of different types

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

}

Luke Warren
A: 

It can be silly since this code somehow eliminates the meaning of generic , however In 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;
    }
Random Charlie