Node class is a generic class that will help to initialize each node of linked list.
public class Node{ public Node(T value) { this.Value = value; } public T Value { get; set; } public Node Next { get; set; } public override string ToString() { return this.Value.ToString(); } }
/// <summary> /// This is a singly linked list and it will allow you to add bunch of Node<T>s /// and that will be linked to each other. /// </summary> /// <typeparam name="T"></typeparam> public class LinkedListwhere T : IEnumerable { public LinkedList(IEnumerable collection) { if (collection == null) { throw new NullReferenceException("collection is null"); } foreach (T item in collection) { this.Add(item); } } public int Count { get; set; } private Node firstNode; public Node FirstNode { get { return firstNode; } private set { firstNode = value; } } private Node lastNode; public Node LastNode { get { return lastNode; } private set { lastNode = value; } } public void AddFirst(T item) { Node newNode = new Node (item); if (this.FirstNode == null) { this.FirstNode = this.LastNode = newNode; } else { newNode.Next = this.FirstNode; this.FirstNode = newNode; } this.Count++; } /// <summary> /// Add will always add the items at last. /// </summary> /// <param name="item"></param> public void Add(T item) { Node newNode = new Node item); if (this.LastNode == null) { this.FirstNode = this.LastNode = newNode; } else { this.LastNode.Next = newNode; this.LastNode = newNode; } this.Count++; } /// <summary> /// Add new item after particular Node<T> /// </summary> /// <param name="node"></param> /// <param name="item"></param> public void AddAfter(Node node, T item) { if (node == null) { return; } Node ; newNode = new Node item); if (node.Next == null) { node.Next = newNode; } else { newNode.Next = node.Next; node.Next = newNode; } this.Count++; } /// <summary> /// Remove the item /// </summary> /// <param name="item"></param> public void Remove(T item) { if (item == null) { throw new NullReferenceException("item is null"); } Node newNode = this.Find(item); this.RemoveTheRef(ref newNode); } /// <summary> /// Remove the first node /// </summary> public void RemoveFirst() { Node first = this.FirstNode; RemoveTheRef(ref first); } /// <summary> /// Remove the last node /// </summary> public void RemoveLast() { Node last = this.LastNode; RemoveTheRef(ref last); } /// <summary> /// Find the item inside list. /// </summary> /// <param name="value"></param> /// <returns></returns> public Node Find(T value) { Node Node = this.FirstNode; while (Node != null) { if (Node.Value.Equals(value)) { return Node; } Node = Node.Next; } return null; } /// /// Find previous Node<T> /// public Node FindPreviousNode(T value) { Node Node = this.FirstNode; Node previousNode = this.FirstNode; while (Node != null) { if (Node.Value.Equals(value)) { return previousNode; } previousNode = Node; Node = Node.Next; } return null; } /// <summary> /// Clear the collection /// </summary> public void Clear() { this.FirstNode = null; this.LastNode = null; this.Count = 0; } /// <summary> /// Get enumerator will help you to traverse the collection /// </summary> /// <returns></returns> IEnumerator GetEnumerator() { Node currentNode = this.FirstNode; while (currentNode != null) { yield return currentNode.Value; currentNode = currentNode.Next; } } /// /// Remove the item from the linked list. /// public void RemoveTheRef(ref Node<T> Node) { if (Node != null) { Node prevNode = FindPreviousNode(Node.Value); if (prevNode != null) { prevNode.Next = Node.Next; } if (Node.Equals(this.FirstNode)) { this.FirstNode = Node.Next; } if (Node.Equals(this.LastNode)) { this.LastNode = prevNode; } } Node = null; this.Count--; } }
Guys, there is some problem in posting generic type. So please Replace Node to Node of T type
Happy Coding :)