Linked List in C#

Linked List is a dynamic data structure consisting of a group of nodes. In each node contains a ref to the next node in the list. In addition, each node contain an object  which contains "data"

Version 1:
Non-Generic Version 

public sealed class Node<T> {


public T m_data;
public Node<T> m_next;

public Node(T data) : this(data, null) {
}

public Node(T data, Node<T> next) {
m_data = data; m_next = next;
}

public override String ToString() {
return m_data.ToString() +
((m_next != null) ? m_next.ToString() : String.Empty);
}
}

Implement:
private static void SameDataLinkedList() {
Node<Char> head = new Node<Char>('C');
head = new Node<Char>('B', head);
head = new Node<Char>('A', head);
Console.WriteLine(head.ToString()); // Displays "ABC"


Version 2: Generic Version
internal class Node {
protected Node m_next;
public Node(Node next) {
m_next = next;
}
}
internal sealed class TypedNode<T> : Node {
public T m_data;
public TypedNode(T data) : this(data, null) {
}
public TypedNode(T data, Node next) : base(next) {
m_data = data;
}
public override String ToString() {
return m_data.ToString() +
((m_next != null) ? m_next.ToString() : String.Empty);
}
}  




Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment