Reputation: 15762
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.
Upvotes: 0
Views: 8096
Reputation: 503
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;
}
}
}
Upvotes: 0
Reputation: 11
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();
}
}
}
}
Upvotes: 1
Reputation: 11
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;
}
Upvotes: 1
Reputation: 17
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();
}
Upvotes: 0
Reputation: 21
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;
}
}
}
Upvotes: 2
Reputation: 1
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;
}
}
}
Upvotes: 0
Reputation: 117029
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?
Upvotes: 2
Reputation: 41378
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.
Upvotes: 0