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