Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp10
- {
- public class SortList<T> where T: IComparable<T>
- {
- public int length;
- List<T> list;
- public SortList(List<T> m)
- {
- list = new List<T>();
- list = m;
- length = m.Count();
- }
- // индексатор
- public T this[int index]
- {
- get
- {
- if (index < 0 || index >= length)
- throw new IndexOutOfRangeException();
- return list[index];
- }
- set
- {
- if (index < 0 || index >= length)
- throw new IndexOutOfRangeException();
- list[index] = value;
- }
- }
- public List<T> Sort()
- {
- for(int i=0;i<length;i++)
- {
- for (int j = i+1; j< length;j++)
- {
- if(list[i].CompareTo(list[j])<0)
- {
- T y = list[j];
- list[j] = list[i];
- list[i] = y;
- }
- }
- }
- return list;
- }
- public List<T> AddForIndex(T m, int y)
- {
- List<T> list2 = new List<T>();
- for(int i=0;i<y;i++)
- {
- list2.Add(list[i]);
- }
- list2[y] = m;
- for(int i=y+1;i<list.Count+1;i++)
- {
- list2.Add(list[i]);
- }
- return list2;
- }
- public List<T> Remove(T m)
- {
- List<T> list2 = new List<T>();
- int pos = 0;
- for (int i=0;i<length;i++)
- {
- if (list[i].CompareTo(m)==0)
- {
- pos = i;
- }
- }
- for (int i = 0; i < pos; i++)
- {
- list2.Add(list[i]);
- }
- for (int i = pos + 1; i < list.Count + 1; i++)
- {
- list2.Add(list[i-1]);
- }
- return list2;
- }
- public void print ()
- {
- for (int i = 0; i < list.Count; i++)
- {
- Console.WriteLine(list[i]);
- }
- }
- public bool Contains(T m)
- {
- bool flag = false;
- for (int i=0;i<length;i++)
- {
- if (list[i].CompareTo(m) == 0)
- flag = true;
- }
- return flag;
- }
- public T IndexOf(int m)
- {
- return list[m];
- }
- public int PosElement(T m)
- {
- int s = 0;
- for(int i=0;i<length;i++)
- {
- if(list[i].CompareTo(m) == 0)
- {
- s++;
- }
- }
- return s;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- List<int> list = new List<int>();
- list.Add(123);
- list.Add(2);
- list.Add(637);
- list.Add(99);
- list.Add(108);
- list.Add(235);
- SortList<int> l = new SortList<int>(list);
- Console.WriteLine( l.Sort());
- Console.WriteLine( l.AddForIndex(67, 2));
- Console.WriteLine( l.Remove(1));
- if(l.Contains(108)==true)
- {
- Console.WriteLine("Такой элемент есть");
- }
- else
- Console.WriteLine("Такого элемента нет");
- Console.WriteLine(l.IndexOf(4));
- Console.WriteLine(l.PosElement(99));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement