Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp37
  9. {
  10. public class SortList<T> where T: IComparable<T>
  11. {
  12. public int length;
  13. List<T> list;
  14. public SortList(List<T> m)
  15. {
  16. list = new List<T>();
  17. for(int i=0;i<m.Count;i++)
  18. {
  19. list.Add(m[i]);
  20. }
  21. length = m.Count();
  22. }
  23. // индексатор
  24. public T this[int index]
  25. {
  26. get
  27. {
  28. if (index < 0 || index >= length)
  29. throw new IndexOutOfRangeException();
  30. return list[index];
  31. }
  32. set
  33. {
  34. if (index < 0 || index >= length)
  35. throw new IndexOutOfRangeException();
  36. list[index] = value;
  37. }
  38. }
  39.  
  40. public List<T> Sort()
  41. {
  42. for(int i=0;i<length;i++)
  43. {
  44. for (int j = i+1; j< length;j++)
  45. {
  46. if(list[i].CompareTo(list[j])>0)
  47. {
  48. T y = list[j];
  49. list[j] = list[i];
  50. list[i] = y;
  51. }
  52.  
  53. }
  54. }
  55. return list;
  56. }
  57.  
  58. public List<T> AddForIndex(T m, int y)
  59. {
  60. List<T> list2 = new List<T>();
  61.  
  62. for(int i=0;i<y;i++)
  63. {
  64. list2.Add(list[i]);
  65. }
  66. list2.Add(m);
  67. for(int i=y;i<list.Count;i++)
  68. {
  69. list2.Add(list[i]);
  70. }
  71. list.Clear();
  72. for(int i=0;i<list2.Count;i++)
  73. {
  74. list.Add(list2[i]);
  75. }
  76. return list;
  77. }
  78. public List<T> Remove(T m)
  79. {
  80. List<T> list2 = new List<T>();
  81. int pos = 0;
  82. for (int i=0;i<length;i++)
  83. {
  84. if (list[i].CompareTo(m)==0)
  85. {
  86. pos = i;
  87. }
  88. }
  89. for (int i = 0; i < pos; i++)
  90. {
  91. list2.Add(list[i]);
  92. }
  93. for (int i = pos+1; i < list.Count+1; i++)
  94. {
  95. list2.Add(list[i-1]);
  96. }
  97. list.Clear();
  98. for (int i = 0; i < list2.Count; i++)
  99. {
  100. list.Add(list2[i]);
  101. }
  102. return list;
  103.  
  104. }
  105. public override string ToString()
  106. {
  107. string str = "";
  108. for(int i=0;i<length; i++)
  109. {
  110. Console.WriteLine(list[i]);
  111. }
  112. return str;
  113. }
  114. public void print()
  115. {
  116. for (int i = 0; i < list.Count; i++)
  117. {
  118. Console.WriteLine(list[i]);
  119. }
  120. }
  121. public bool Contains(T m)
  122. {
  123. bool flag = false;
  124. for (int i=0;i<length;i++)
  125. {
  126. if (list[i].CompareTo(m) == 0)
  127. flag = true;
  128. }
  129. return flag;
  130. }
  131. public T IndexOf(int m)
  132. {
  133. return list[m];
  134. }
  135. public int PosElement(T m)
  136. {
  137. int s = 0;
  138. for(int i=0;i<length;i++)
  139. {
  140. if(list[i].CompareTo(m) == 0)
  141. {
  142. s++;
  143. }
  144. }
  145. return s;
  146. }
  147. }
  148. class Program
  149. {
  150. static void Main(string[] args)
  151. {
  152. List<int> list = new List<int>();
  153. list.Add(123);
  154. list.Add(2);
  155. list.Add(637);
  156. list.Add(99);
  157. list.Add(108);
  158. list.Add(235);
  159. SortList<int> l = new SortList<int>(list);
  160. l.Sort();
  161. l.print();
  162. Console.WriteLine("-------------------");
  163. l.AddForIndex(67, 2);
  164.  
  165. l.print();
  166. Console.WriteLine("-------------------");
  167. l.Remove(1);
  168. l.print();
  169. Console.WriteLine("-------------------");
  170. if (l.Contains(108)==true)
  171. {
  172. Console.WriteLine("Такой элемент есть");
  173. }
  174. else
  175. Console.WriteLine("Такого элемента нет");
  176.  
  177. Console.WriteLine(l.IndexOf(4));
  178. Console.WriteLine(l.PosElement(99));
  179. Console.ReadKey();
  180.  
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement