Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2.  
  3. namespace LinkedList
  4. {
  5.     public static class Helpers
  6.     {
  7.         public static void InsertItem<T>(ref Item<T> start, T val, Func<T, Item<T>, bool> insertBefore)
  8.         {
  9.             Console.WriteLine($"Creating item: {val}");
  10.             Item<T> current = start, previous = null;
  11.  
  12.             while (current != null && !insertBefore(val, current))
  13.             {
  14.                 previous = current;
  15.                 current = current.next;
  16.             }
  17.             Item<T> item = new Item<T>(val, current);
  18.  
  19.             if (previous == null)
  20.             {
  21.                 start = item;
  22.             }
  23.             else
  24.             {
  25.                 previous.next = item;
  26.             }
  27.         }
  28.  
  29.         public static void RemoveItem<T>(ref Item<T> start, T val, Func<Item<T>, T, bool> valueEquals)
  30.         {
  31.             Item<T> current = start, previous = null;
  32.  
  33.             while (current != null && !valueEquals(current, val))
  34.             {
  35.                 previous = current;
  36.                 current = current.next;
  37.             }
  38.  
  39.             if (current == null)
  40.             {
  41.                 Console.WriteLine($"Item {val} does not exist!");
  42.             }
  43.             else
  44.             {
  45.                 if (previous == null)
  46.                 {
  47.                     start = current.next;
  48.                 }
  49.                 else
  50.                 {
  51.                     previous.next = current.next;
  52.                 }
  53.                 Console.WriteLine($"Removed item: {val}");
  54.             }
  55.         }
  56.  
  57.         public static void RemoveAll<T>(ref Item<T> start) => start = null;
  58.  
  59.         public static void PrintList<T>(Item<T> start)
  60.         {
  61.             while (start != null)
  62.             {
  63.                 start = start.PrintGetNext();
  64.             }
  65.         }
  66.  
  67.         public static void PrintIterator<T>(Item<T> start)
  68.         {
  69.             if (start != null)
  70.             {
  71.                 foreach (var item in start.GetIterator())
  72.                 {
  73.                     item.PrintGetNext();
  74.                 }
  75.             }
  76.         }
  77.  
  78.         public static void PrintArray<T>(Item<T> start)
  79.         {
  80.             var item = start;
  81.             for (int i = 0; item != null; ++i)
  82.             {
  83.                 item = start[i].PrintGetNext();
  84.             }
  85.         }
  86.  
  87.         public static void PrintRecursive<T>(Item<T> start)
  88.         {
  89.             if (start != null)
  90.             {
  91.                 PrintRecursive(start.PrintGetNext());
  92.             }
  93.         }
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement