fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace List
  8. {
  9. class CustomList<T> : IList<T>
  10. {
  11. private class Node
  12. {
  13. public T Item { get; set; }
  14. public int Index { get; set; }
  15. public Node Next { get; set; }
  16. }
  17.  
  18. private Node root;
  19. private int count;
  20. private bool isReadOnly;
  21.  
  22. public int IndexOf(T item)
  23. {
  24. for (Node n = this.root; n != null; n = n.Next)
  25. if (n.Item.GetHashCode() == item.GetHashCode()) return n.Index;
  26. return -1;
  27. }
  28.  
  29. public void Insert(int index, T item)
  30. {
  31. this.isReadOnly = true;
  32. for (Node n = this.root; n != null; n = n.Next)
  33. {
  34. if (n.Index == index)
  35. {
  36. this.count += 1;
  37. var node = new Node { Item = item };
  38. node.Next = n.Next;
  39. n.Next = node;
  40. this.Reindex();
  41. break;
  42. }
  43. }
  44. this.isReadOnly = false;
  45. }
  46.  
  47. public void RemoveAt(int index)
  48. {
  49. Node temp = this.root;
  50. Node preview = null;
  51.  
  52. this.isReadOnly = true;
  53. while (temp.Next != null)
  54. {
  55. preview = temp;
  56. temp = temp.Next;
  57.  
  58. if (temp.Index == index)
  59. {
  60. this.count -= 1;
  61. if (preview != null)
  62. {
  63. preview.Next = temp.Next;
  64. this.Reindex();
  65. }
  66. else this.root = temp.Next;
  67. }
  68. }
  69. this.isReadOnly = false;
  70. }
  71.  
  72. public T this[int index]
  73. {
  74. get
  75. {
  76. for (Node n = this.root; n != null; n = n.Next)
  77. if (n.Index == index) return n.Item;
  78. return default(T);
  79. }
  80. set
  81. {
  82. for (Node n = this.root; n != null; n = n.Next)
  83. if (n.Index == index) n.Item = value;
  84. }
  85. }
  86.  
  87. public void Add(T item)
  88. {
  89. var node = new Node { Item = item, Index = this.root != null ? this.root.Index + 1 : 0 };
  90. node.Next = this.root;
  91. this.root = node;
  92. this.count += 1;
  93. }
  94.  
  95. public void Clear()
  96. {
  97. this.root = null;
  98. }
  99.  
  100. public bool Contains(T item)
  101. {
  102. for (Node n = this.root; n != null; n = n.Next)
  103. if (n.Item.GetHashCode() == item.GetHashCode()) return true;
  104. return false;
  105. }
  106.  
  107. public void CopyTo(T[] array, int arrayIndex = 0)
  108. {
  109. var index = arrayIndex;
  110. for (Node n = this.root; n != null; n = n.Next)
  111. {
  112. array[index++] = n.Item;
  113. }
  114. }
  115.  
  116. public int Count
  117. {
  118. get { return this.count; }
  119. }
  120.  
  121. public bool IsReadOnly
  122. {
  123. get { return this.isReadOnly; }
  124. }
  125.  
  126. public bool Remove(T item)
  127. {
  128. Node temp = this.root;
  129. Node preview = null;
  130.  
  131. this.isReadOnly = true;
  132. while (temp.Item.GetHashCode() != item.GetHashCode() && temp.Next != null)
  133. {
  134. preview = temp;
  135. temp = temp.Next;
  136. }
  137.  
  138. if (temp.Item.GetHashCode() == item.GetHashCode())
  139. {
  140. this.count -= 1;
  141. if (preview != null)
  142. {
  143. preview.Next = temp.Next;
  144. this.Reindex();
  145. }
  146. else this.root = temp.Next;
  147. this.isReadOnly = false;
  148. return true;
  149. }
  150.  
  151. this.isReadOnly = false;
  152. return false;
  153. }
  154.  
  155. public IEnumerator<T> GetEnumerator()
  156. {
  157. for (Node n = this.root; n != null; n = n.Next) yield return n.Item;
  158. }
  159.  
  160. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  161. {
  162. return this.GetEnumerator();
  163. }
  164.  
  165. public void Print()
  166. {
  167. for (Node n = this.root; n != null; n = n.Next)
  168. if (n.Item is string) Console.WriteLine("{0}: {1}", n.Index, n.Item);
  169. Console.WriteLine();
  170. }
  171.  
  172. public void Reindex()
  173. {
  174. var index = this.count - 1;
  175. for (Node n = this.root; n != null; n = n.Next)
  176. n.Index = index--;
  177. }
  178. }
  179.  
  180. class Program
  181. {
  182. static void Main(string[] args)
  183. {
  184. var list = new CustomList<string>() { "Tomek", "Rafał", "Maciek", "Grzegorz", "Anna", "Kasia", "Stasia" };
  185. list.Print();
  186.  
  187. list.Insert(0, "Wstawiony zerowy");
  188. list.Insert(3, "Wstawiony trzeci");
  189.  
  190. list.Remove("Maciek");
  191. list.Print();
  192.  
  193. list.Remove("Stasia");
  194. list.Remove("Kasia");
  195. list.Remove("Grzegorz");
  196. list.Print();
  197.  
  198. list.RemoveAt(1);
  199. list.RemoveAt(0);
  200. list.Print();
  201.  
  202. list.Add("Karol");
  203. list.Add("Piotrek");
  204. list.Add("Stefan");
  205. list.Add("Sebastian");
  206. list.Add("Monika");
  207. list[3] = "Zmieniony Stefan";
  208. list[0] = "Jakiś Zenon";
  209. list.Print();
  210.  
  211. list.RemoveAt(list.Count - 1);
  212. list.RemoveAt(0);
  213. list.Print();
  214.  
  215. Console.WriteLine("Index of Monika: {0}", list.IndexOf("Monika"));
  216. Console.WriteLine("Contains Piotrek: {0}", list.Contains("Piotrek"));
  217. Console.WriteLine("Contains Blabla: {0}", list.Contains("Blabla"));
  218.  
  219. var array = new string[list.Count + 10];
  220. list.CopyTo(array, 4);
  221. for (int i = 0; i < array.Length; i++) Console.WriteLine("{0}: {1}", i, array[i]);
  222. }
  223. }
  224. }
  225.  
Success #stdin #stdout 0s 29672KB
stdin
Standard input is empty
stdout
6: Stasia
5: Kasia
4: Anna
3: Grzegorz
2: Maciek
1: Rafał
0: Tomek

7: Stasia
6: Kasia
5: Anna
4: Grzegorz
3: Wstawiony trzeci
2: Rafał
1: Tomek
0: Wstawiony zerowy

4: Anna
3: Wstawiony trzeci
2: Rafał
1: Tomek
0: Wstawiony zerowy

2: Anna
1: Wstawiony trzeci
0: Rafał

7: Monika
6: Sebastian
5: Stefan
4: Piotrek
3: Zmieniony Stefan
2: Anna
1: Wstawiony trzeci
0: Jakiś Zenon

6: Monika
5: Sebastian
4: Stefan
3: Piotrek
2: Zmieniony Stefan
1: Anna
0: Wstawiony trzeci

Index of Monika: 6
Contains Piotrek: True
Contains Blabla: False
0: 
1: 
2: 
3: 
4: Monika
5: Sebastian
6: Stefan
7: Piotrek
8: Zmieniony Stefan
9: Anna
10: Wstawiony trzeci
11: 
12: 
13: 
14: 
15: 
16: