simonradev

LinkedList

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