diff --git a/README.md b/README.md index fac4ff01..b4dc45a8 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa |:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:| | [AVL Tree](http://www.geeksforgeeks.org/avl-tree-set-1-insertion)| | |[:white_check_mark:](avl_tree/AvlTree.java) | | | | | | [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | | [:white_check_mark:](binary_search_tree/BinarySearchTree.java) | | | | | -| [Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:white_check_mark:](linked_list/linkedList.c.c) | | [:white_check_mark:](linked_list/LinkedList.java) | [:white_check_mark:](linked_list/linked_list.py) | [:white_check_mark:](linked_list/linked_list.go) | [:white_check_mark:](linked_list/linkedList.js) | | +| [Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:white_check_mark:](linked_list/linkedList.c.c) | | [:white_check_mark:](linked_list/LinkedList.java) | [:white_check_mark:](linked_list/linked_list.py) | [:white_check_mark:](linked_list/linked_list.go) | [:white_check_mark:](linked_list/linkedList.js) | [:white_check_mark:](linked_list/LinkedList.cs) | | [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:white_check_mark:](stack/stack.c) | | [:white_check_mark:](stack/Stack.java) | [:white_check_mark:](stack/stack.py) | [:white_check_mark:](stack/stack.go) | [:white_check_mark:](stack/stack.js) | | | [Trie](https://en.wikipedia.org/wiki/Trie) | | [:white_check_mark:](trie/trie.cpp) | [:white_check_mark:](trie/Trie.java) | | | | | | diff --git a/linked_list/LinkedList.cs b/linked_list/LinkedList.cs new file mode 100644 index 00000000..51061e9b --- /dev/null +++ b/linked_list/LinkedList.cs @@ -0,0 +1,240 @@ +using System; + +public class RunLinkedList +{ + public static void Main(string[] args) + { + LinkedList list = new LinkedList(); + + try + { + Console.WriteLine(list.RemoveLast()); + } + catch (InvalidOperationException) + { + Console.WriteLine("The list is empty"); + } + + list.Add(2, 0); + Console.WriteLine(list); // [2] + + list.Remove(0); + Console.WriteLine(list); // Empty + + list.AddFront(2); + list.Add(5, 1); + list.Add(10, 2); + list.AddLast(3); + Console.WriteLine(list); // [2, 5, 10, 3] + + Console.WriteLine(list.Search(5)); // 1 + Console.WriteLine(list.Search(0)); // -1 + + list.RemoveLast(); + Console.WriteLine(list); // [2, 5, 10] + + list.RemoveFront(); + Console.WriteLine(list); // [5, 10] + + Console.WriteLine(list.IsEmpty()); // False + + while (!list.IsEmpty()) + { + list.RemoveLast(); + } + + Console.WriteLine(list.IsEmpty()); // True + + Console.ReadKey(); + } +} + +public class LinkedList +{ + public class Node + { + public U data; + public Node next; + + public Node(U obj) + { + this.data = obj; + this.next = null; + } + } + + private Node head; + public int size = 0; + + public LinkedList() + { + this.head = null; + } + + public void AddFront(T obj) + { + Node node = new Node(obj); + node.next = this.head; + this.head = node; + this.size++; + } + + public void AddLast(T obj) + { + if (this.head == null) + { + Node node = new Node(obj); + this.head = node; + this.size = 1; + } + else + { + Node temp = this.head; + while (temp.next != null) + { + temp = temp.next; + } + Node node = new Node(obj); + temp.next = node; + size++; + } + } + + public void Add(T obj, int index) + { + if (index < 0 || index > this.size) + { + throw new Exception("Invalid Index"); + } + Node temp = this.head; + if (index == 0) + { + AddFront(obj); + } + else + { + for (int i = 0; i < index - 1; i++) + { + temp = temp.next; + } + Node node = new Node(obj); + node.next = temp.next; + temp.next = node; + this.size++; + } + } + + public T RemoveFront() + { + if (IsEmpty()) + { + throw new InvalidOperationException(); + } + T objRemoved = this.head.data; + this.head = this.head.next; + this.size--; + return objRemoved; + } + + public T Remove(int index) + { + if (IsEmpty()) + { + throw new InvalidOperationException(); + } + else if (index < 0 || index > this.size) + { + throw new IndexOutOfRangeException("Invalid Index"); + } + else if (index > 0) + { + Node temp = this.head; + for (int i = 0; i < index - 1; i++) + { + temp = temp.next; + } + T objRemoved = temp.next.data; + temp.next = temp.next.next; + this.size--; + return objRemoved; + } + return RemoveFront(); + } + + public T RemoveLast() + { + if (IsEmpty()) + { + throw new InvalidOperationException(); + } + T objRemoved; + if (this.head.next == null) + { + objRemoved = this.head.data; + this.head = null; + } + else + { + Node temp = this.head; + while (temp.next.next != null) + { + temp = temp.next; + } + objRemoved = temp.next.data; + temp.next = null; + } + this.size--; + return objRemoved; + } + + public int Search(T obj) + { + Node temp = this.head; + for (int i = 0; i < this.size; i++) + { + if (temp.data.Equals(obj)) + { + return i; + } + else + { + temp = temp.next; + } + } + return -1; + } + + public int GetSize() + { + return this.size; + } + + public bool IsEmpty() + { + return size == 0; + } + + public override string ToString() + { + string returnString = ""; + Node temp = this.head; + if (temp != null) + { + returnString += "["; + while (temp != null) + { + if (temp.next == null) + { + return returnString += temp.data + "]"; + } + else + { + returnString += temp.data + ", "; + temp = temp.next; + } + } + return returnString; + } + return "[]"; + } +}