Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Test
  4. {
  5.     class List<T>
  6.     {
  7.         public List(int capacity = 10)
  8.         {
  9.             Data = new ListItem<T>[capacity];
  10.             FreeCells = new int[capacity];
  11.             Head = -1;
  12.             Tail = -1;
  13.             Free = 0;
  14.             Capacity = capacity;
  15.             FreeCells[FreeCells.Length - 1] = -1;
  16.             for (int i = 0; i < FreeCells.Length - 1; i++)
  17.             {
  18.                 FreeCells[i] = i + 1;
  19.             }
  20.         }
  21.  
  22.         public void Add(T value)
  23.         {
  24.             Data[Free].Value = value;
  25.             if (Head == -1)
  26.             {
  27.                 Head = Free;
  28.                 Tail = Free;
  29.             }
  30.             Data[Tail].Next = Free;
  31.             Tail = Free;
  32.             Free = FreeCells[Free];
  33.         }
  34.  
  35.         public int IndexOf(T value)
  36.         {
  37.             if (Head == -1)
  38.             {
  39.                 return -1;
  40.             }
  41.  
  42.             ListItem<T> current = Data[Head];
  43.             int i = Head;
  44.  
  45.             while (i != -1)
  46.             {
  47.                 if (Data[i].Value.Equals(value))
  48.                 {
  49.                     return i;
  50.                 }
  51.                 i = current.Next;
  52.                 current = Data[current.Next];
  53.             }
  54.  
  55.             return i;
  56.         }
  57.  
  58.         public void Remove(T value)
  59.         {
  60.             int i = IndexOf(value);
  61.  
  62.             if (i == Head)
  63.             {
  64.                 if (Head == Tail)
  65.                 {
  66.                     Head = -1;
  67.                     Tail = -1;
  68.                 }
  69.                 else
  70.                 {
  71.                     Head = i + 1;
  72.                 }
  73.             }
  74.             else if (i == Tail)
  75.             {
  76.                 if (Head == Tail)
  77.                 {
  78.                     Head = -1;
  79.                     Tail = -1;
  80.                 }
  81.                 else
  82.                 {
  83.                     Tail = i - 1;
  84.                     Data[i - 1].Next = -1;
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 Data[i - 1].Next = Data[i + 1].Next;
  90.             }
  91.  
  92.             FreeCells[i] = Free;
  93.             Free = i;
  94.         }
  95.  
  96.         public void RemoveAt(int index)
  97.         {
  98.             if (Head == -1)
  99.             {
  100.                 throw new IndexOutOfRangeException();
  101.             }
  102.  
  103.             int count = 0;
  104.             ListItem<T> current = Data[Head];
  105.             int i = Head;
  106.             int prev = Head;
  107.  
  108.             while (count != index)
  109.             {
  110.                 if (i == -1)
  111.                 {
  112.                     throw new IndexOutOfRangeException();
  113.                 }
  114.  
  115.                 prev = i;
  116.  
  117.                 i = current.Next;
  118.                 current = Data[current.Next];
  119.                 count++;
  120.             }
  121.  
  122.             Data[prev].Next = Data[i].Next;
  123.             FreeCells[i] = Free;
  124.             Free = i;
  125.         }
  126.  
  127.         public void Clear()
  128.         {
  129.             Data = new ListItem<T>[Capacity];
  130.             FreeCells = new int[Capacity];
  131.             Head = -1;
  132.             Tail = -1;
  133.             Free = 0;
  134.             FreeCells[FreeCells.Length - 1] = -1;
  135.             for (int i = 0; i < FreeCells.Length - 1; i++)
  136.             {
  137.                 FreeCells[i] = i + 1;
  138.             }
  139.         }
  140.  
  141.         public T this[int index]
  142.         {
  143.             get
  144.             {
  145.                 int count = 0;
  146.                 ListItem<T> current = Data[Head];
  147.                 int i = Head;
  148.  
  149.                 while (count != index)
  150.                 {
  151.                     if (i == -1)
  152.                     {
  153.                         throw new IndexOutOfRangeException();
  154.                     }
  155.                     i = current.Next;
  156.                     current = Data[current.Next];
  157.                     count++;
  158.                 }
  159.  
  160.                 return Data[i].Value;
  161.             }
  162.         }
  163.  
  164.         private struct ListItem<T>
  165.         {
  166.             public T Value;
  167.             public int Next;
  168.         }
  169.  
  170.         private ListItem<T>[] Data;
  171.         private int[] FreeCells;
  172.         private int Free;
  173.         private int Head;
  174.         private int Tail;
  175.         private int Capacity;
  176.     }
  177.  
  178.     class Program
  179.     {
  180.         public static void Main(string[] args)
  181.         {
  182.             List<int> list = new List<int>();
  183.  
  184.             list.Add(5);
  185.             list.Add(7);
  186.             list.Add(3);
  187.             list.Add(1);
  188.             list.Add(55);
  189.  
  190.             for (int i = 0; i < 5; i++)
  191.             {
  192.                 Console.WriteLine(list[i]);
  193.             }
  194.  
  195.             for (int i = 4; i >= 0; i--)
  196.             {
  197.                 list.RemoveAt(i);
  198.             }
  199.  
  200.             list.Add(3);
  201.             Console.WriteLine("new {0}", list[0]);
  202.  
  203.             list.Remove(3);
  204.  
  205.             list.Add(5);
  206.             Console.WriteLine("new2 {0}", list[0]);
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement