simonradev

LinkedList

Dec 21st, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace LinkedListTest
  8. {
  9.     class LinkedListTest
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //avaliable commands - Add,AddRange,InsertAt,InsertRange,Remove,RemoveAt,RemoveRange,ElementAt,IndexOf,GetAllElements
  14.  
  15.             LinkedList testList = new LinkedList();
  16.  
  17.             testList.Add("one");
  18.             testList.Add("two");
  19.             testList.Add("three");
  20.             testList.Add("four");
  21.             testList.Add("five");
  22.  
  23.             object[] elements = testList.GetAllElements();
  24.         }
  25.     }
  26.  
  27.     class Node
  28.     {
  29.         private Node next;
  30.         private object currValue;
  31.  
  32.         public Node Next
  33.         {
  34.             get { return this.next; }
  35.             set { this.next = value; }
  36.         }
  37.  
  38.         public object Value
  39.         {
  40.             get { return this.currValue; }
  41.             set { this.currValue = value; }
  42.         }
  43.     }
  44.  
  45.     class LinkedList
  46.     {
  47.         private Node head;
  48.         private int count;
  49.  
  50.         public int Count { get { return this.count; } }
  51.  
  52.         public void Add(object element)
  53.         {
  54.             if (head == null)
  55.             {
  56.                 head = new Node();
  57.                 head.Next = null;
  58.                 head.Value = element;
  59.             }
  60.             else
  61.             {
  62.                 Node toAdd = new Node();
  63.                 toAdd.Value = element;
  64.  
  65.                 Node currNode = head;
  66.                 while (currNode.Next != null)
  67.                 {
  68.                     currNode = currNode.Next;
  69.                 }
  70.  
  71.                 currNode.Next = toAdd;
  72.             }
  73.  
  74.             count++;
  75.         }
  76.  
  77.         public void AddRange(object[] items)
  78.         {
  79.             foreach (object item in items)
  80.             {
  81.                 Add(item);
  82.             }
  83.         }
  84.  
  85.         public void InsertAt(object element, int index)
  86.         {
  87.             if (count != 0 && (index < 0 || index > count))
  88.             {
  89.                 string message = string.Format
  90.                     ("You tried to reach index [{0}] but the index of the last element is [{1}]", index, count - 1);
  91.                 throw new IndexOutOfRangeException(message);
  92.             }
  93.  
  94.             if (head != null && index == 0)
  95.             {
  96.                 Node toInsert = new Node();
  97.                 toInsert.Value = element;
  98.                 toInsert.Next = head;
  99.  
  100.                 head = toInsert;
  101.             }
  102.             else if (head == null)
  103.             {
  104.                 head = new Node();
  105.                 head.Value = element;
  106.                 head.Next = null;
  107.             }
  108.             else
  109.             {
  110.                 Node toInsert = new Node();
  111.                 toInsert.Value = element;
  112.  
  113.                 int indexCounter = 0;
  114.                 Node currNode = head;
  115.                 while (currNode.Next != null && indexCounter + 1 != index)
  116.                 {
  117.                     currNode = currNode.Next;
  118.                     indexCounter++;
  119.                 }
  120.  
  121.                 toInsert.Next = currNode.Next;
  122.                 currNode.Next = toInsert;
  123.                
  124.             }
  125.  
  126.             count++;
  127.         }
  128.  
  129.         public void InsertRange(object[] items, int index)
  130.         {
  131.             foreach (object item in items)
  132.             {
  133.                 InsertAt(item, index);
  134.                 index++;
  135.             }
  136.         }
  137.  
  138.         public void Remove(object item)
  139.         {
  140.             bool containsItem = false;
  141.  
  142.             Node currNode = head;
  143.             Node prevNode = null;
  144.             while (currNode.Next != null)
  145.             {
  146.                 if (currNode.Value.Equals(item))
  147.                 {
  148.                     containsItem = true;
  149.                     break;
  150.                 }
  151.                 prevNode = currNode;
  152.                 currNode = currNode.Next;
  153.             }
  154.  
  155.             if (!containsItem && currNode.Value.Equals(item))
  156.             {
  157.                 containsItem = true;
  158.             }
  159.  
  160.             if (!containsItem && head.Value.Equals(item))
  161.             {
  162.                 head = null;
  163.             }
  164.             else if (!containsItem)
  165.             {
  166.                 throw new InvalidOperationException($"Object [{item.ToString()}] does not exist in the list.");
  167.             }
  168.             else if (containsItem && head.Value.Equals(item))
  169.             {
  170.                 head = currNode.Next;
  171.             }
  172.             else
  173.             {
  174.                 prevNode.Next = currNode.Next;
  175.                 currNode = null;
  176.             }
  177.  
  178.             count--;
  179.         }
  180.  
  181.         public void RemoveAt(int index)
  182.         {
  183.             if (index < 0 || index >= count)
  184.             {
  185.                 string message = string.Format
  186.                     ("You tried to reach index [{0}] but the index of the last element is [{1}]", index, count - 1);
  187.                 throw new IndexOutOfRangeException(message);
  188.             }
  189.  
  190.             bool indexIsFound = false;
  191.             Node prevNode = null;
  192.             Node currNode = head;
  193.             int indexCounter = 0;
  194.             while (currNode.Next != null)
  195.             {
  196.                 if (index == indexCounter)
  197.                 {
  198.                     indexIsFound = true;
  199.                     break;
  200.                 }
  201.  
  202.                 prevNode = currNode;
  203.                 currNode = currNode.Next;
  204.                 indexCounter++;
  205.             }
  206.  
  207.             if (!indexIsFound && index == indexCounter)
  208.             {
  209.                 indexIsFound = true;
  210.             }
  211.  
  212.             if (indexIsFound && index == 0)
  213.             {
  214.                 head = currNode.Next;
  215.             }
  216.             else if (indexIsFound)
  217.             {
  218.                 prevNode.Next = currNode.Next;
  219.                 currNode = null;
  220.             }
  221.  
  222.             count--;
  223.         }
  224.  
  225.         public void RemoveRange(object[] items)
  226.         {
  227.             foreach (object item in items)
  228.             {
  229.                 Remove(item);
  230.             }
  231.         }
  232.  
  233.         public object ElementAt(int index)
  234.         {
  235.             if (index < 0 || index >= count)
  236.             {
  237.                 string message = string.Format
  238.                     ("You tried to reach index [{0}] but the index of the last element is [{1}]", index, count - 1);
  239.                 throw new IndexOutOfRangeException(message);
  240.             }
  241.  
  242.             Node currNode = head;
  243.             bool searchIsFinished = false;
  244.             int indexCounter = 0;
  245.             while (!searchIsFinished)
  246.             {
  247.                 if (currNode == null)
  248.                 {
  249.                     searchIsFinished = true;
  250.                     break;
  251.                 }
  252.  
  253.                 if (indexCounter == index)
  254.                 {
  255.                     return currNode.Value;
  256.                 }
  257.  
  258.                 currNode = currNode.Next;
  259.                 indexCounter++;
  260.             }
  261.  
  262.             return currNode.Value;
  263.         }
  264.  
  265.         public int IndexOf(object item)
  266.         {
  267.             bool itemExists = false;
  268.  
  269.             Node currNode = head;
  270.             int indexCount = 0;
  271.             while (currNode != null)
  272.             {
  273.                 if (currNode.Value.Equals(item))
  274.                 {
  275.                     itemExists = true;
  276.                     break;
  277.                 }
  278.  
  279.                 currNode = currNode.Next;
  280.                 indexCount++;
  281.             }
  282.  
  283.             if (itemExists)
  284.             {
  285.                 return indexCount;
  286.             }
  287.             else
  288.             {
  289.                 throw new InvalidOperationException($"Item [{item.ToString()}] does not exist.");
  290.             }
  291.         }
  292.  
  293.         public object[] GetAllElements()
  294.         {
  295.             object[] toReturn = new object[count];
  296.  
  297.             Node currNode = head;
  298.             int indexOfArr = 0;
  299.             while (currNode != null)
  300.             {
  301.                 toReturn[indexOfArr] = currNode.Value;
  302.  
  303.                 indexOfArr++;
  304.                 currNode = currNode.Next;
  305.             }
  306.  
  307.             return toReturn;
  308.         }
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment