Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinkedList [C#] #475

Merged
merged 13 commits into from
Oct 6, 2017
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | | | | |

Expand Down
240 changes: 240 additions & 0 deletions linked_list/LinkedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
using System;

public class RunLinkedList
{
public static void Main(string[] args)
{
LinkedList<int> list = new LinkedList<int>();

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<T>
{
public class Node<U>
{
public U data;
public Node<U> next;

public Node(U obj)
{
this.data = obj;
this.next = null;
}
}

private Node<T> head;
public int size = 0;

public LinkedList()
{
this.head = null;
}

public void AddFront(T obj)
{
Node<T> node = new Node<T>(obj);
node.next = this.head;
this.head = node;
this.size++;
}

public void AddLast(T obj)
{
if (this.head == null)
{
Node<T> node = new Node<T>(obj);
this.head = node;
this.size = 1;
}
else
{
Node<T> temp = this.head;
while (temp.next != null)
{
temp = temp.next;
}
Node<T> node = new Node<T>(obj);
temp.next = node;
size++;
}
}

public void Add(T obj, int index)
{
if (index < 0 || index > this.size)
{
throw new Exception("Invalid Index");
}
Node<T> temp = this.head;
if (index == 0)
{
AddFront(obj);
}
else
{
for (int i = 0; i < index - 1; i++)
{
temp = temp.next;
}
Node<T> node = new Node<T>(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<T> 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<T> 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<T> 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<T> 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 "[]";
}
}