Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 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. list = m;
  17. length = m.Count();
  18. }
  19. // индексатор
  20. public T this[int index]
  21. {
  22. get
  23. {
  24. if (index < 0 || index >= length)
  25. throw new IndexOutOfRangeException();
  26. return list[index];
  27. }
  28. set
  29. {
  30. if (index < 0 || index >= length)
  31. throw new IndexOutOfRangeException();
  32. list[index] = value;
  33. }
  34. }
  35.  
  36. public List<T> Sort()
  37. {
  38. for(int i=0;i<length;i++)
  39. {
  40. for (int j = i+1; j< length;j++)
  41. {
  42. if(list[i].CompareTo(list[j])<0)
  43. {
  44. T y = list[j];
  45. list[j] = list[i];
  46. list[i] = y;
  47. }
  48.  
  49. }
  50. }
  51. return list;
  52. }
  53. public List<T> AddForIndex(T m, int y)
  54. {
  55. List<T> list2 = new List<T>();
  56.  
  57. for(int i=0;i<y;i++)
  58. {
  59. list2.Add(list[i]);
  60. }
  61. list2[y] = m;
  62. for(int i=y+1;i<list.Count+1;i++)
  63. {
  64. list2.Add(list[i]);
  65. }
  66. return list2;
  67. }
  68. public List<T> Remove(T m)
  69. {
  70. List<T> list2 = new List<T>();
  71. int pos = 0;
  72. for (int i=0;i<length;i++)
  73. {
  74. if (list[i].CompareTo(m)==0)
  75. {
  76. pos = i;
  77. }
  78. }
  79. for (int i = 0; i < pos; i++)
  80. {
  81. list2.Add(list[i]);
  82. }
  83. for (int i = pos + 1; i < list.Count + 1; i++)
  84. {
  85. list2.Add(list[i-1]);
  86. }
  87. return list2;
  88.  
  89. }
  90. public void print ()
  91. {
  92. for (int i = 0; i < list.Count; i++)
  93. {
  94. Console.WriteLine(list[i]);
  95. }
  96. }
  97. public bool Contains(T m)
  98. {
  99. bool flag = false;
  100. for (int i=0;i<length;i++)
  101. {
  102. if (list[i].CompareTo(m) == 0)
  103. flag = true;
  104. }
  105. return flag;
  106. }
  107. public T IndexOf(int m)
  108. {
  109. return list[m];
  110. }
  111. public int PosElement(T m)
  112. {
  113. int s = 0;
  114. for(int i=0;i<length;i++)
  115. {
  116. if(list[i].CompareTo(m) == 0)
  117. {
  118. s++;
  119. }
  120. }
  121. return s;
  122. }
  123. }
  124. class Program
  125. {
  126. static void Main(string[] args)
  127. {
  128. List<int> list = new List<int>();
  129. list.Add(123);
  130. list.Add(2);
  131. list.Add(637);
  132. list.Add(99);
  133. list.Add(108);
  134. list.Add(235);
  135. SortList<int> l = new SortList<int>(list);
  136. Console.WriteLine( l.Sort());
  137. Console.WriteLine( l.AddForIndex(67, 2));
  138. Console.WriteLine( l.Remove(1));
  139. if(l.Contains(108)==true)
  140. {
  141. Console.WriteLine("Такой элемент есть");
  142. }
  143. else
  144. Console.WriteLine("Такого элемента нет");
  145.  
  146. Console.WriteLine(l.IndexOf(4));
  147. Console.WriteLine(l.PosElement(99));
  148.  
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement