Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.30 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace laba6._3
  9. {
  10.     public class Node<T>
  11.     {
  12.         public T Data { get; set; }
  13.         public Node(T data)
  14.         {
  15.             Data = data;
  16.         }
  17.         public Node<T> Next { get; set; }
  18.     }
  19.     public class LinkedList<T>
  20.     {
  21.        
  22.        public Node<T> head; // головной/первый элемент
  23.         Node<T> tail; // последний/хвостовой элемент //pnext
  24.         int count;  // количество элементов в списке
  25.                     // добавление элемента
  26.                     public int s()
  27.         {
  28.             return count;
  29.         }
  30.         public void Add(T data)
  31.         {
  32.             Node<T> node = new Node<T>(data);
  33.  
  34.             if (head == null)
  35.                 head = node;
  36.             else
  37.                 tail.Next = node;
  38.             tail = node;
  39.             count++;
  40.         }
  41.         public void Show()
  42.         {
  43.             Node<T> temp = this.head;
  44.             while(temp.Next!=null)
  45.             {
  46.                
  47.                 temp = temp.Next;
  48.                 Console.WriteLine(temp.Data);
  49.  
  50.             }
  51.         }
  52.         public static LinkedList<T> operator -- (LinkedList<T> a)
  53.         {
  54.             Node<T> previos = a.head;
  55.             int k;
  56.             k = a.count;
  57.             for (int i = 0; i < (k-1)-1; i++)
  58.             {
  59.                 previos = previos.Next;
  60.             }
  61.  
  62.             Node<T> toDelete = previos.Next;
  63.  
  64.             previos.Next = toDelete.Next;
  65.  
  66.             toDelete=null;
  67.            
  68.             return a;
  69.         }
  70.         public static LinkedList<T> operator + (LinkedList<T> a, T k)
  71.         {
  72.            if (a.head==null)
  73.             {
  74.                 a.head = new Node<T>(k);
  75.             }
  76.             Node<T> current = a.head;
  77.             while(current.Next!=null)
  78.             {
  79.                 current = current.Next;
  80.             }
  81.             current.Next = new Node<T>(k);
  82.            
  83.             return a;
  84.         }
  85.         public static LinkedList<T> operator + (LinkedList<T> a, LinkedList<T> a2)
  86.         {
  87.             Node<T> current = a.head;
  88.             Node<T> temp = a2.head;
  89.             while (current.Next != null)
  90.                 current = current.Next;
  91.             current.Next = new Node<T>(a2.head.Data);
  92.             current = current.Next;
  93.             while(temp.Next!=null)
  94.             {
  95.                 current.Next = new Node<T>(temp.Next.Data);
  96.                 temp = temp.Next;
  97.                 current = current.Next;
  98.                
  99.             }
  100.             return a;
  101.         }
  102.         public static LinkedList<T> operator ~ (LinkedList<T> a)
  103.         {
  104.             int counter = 0;
  105.            // int index, index1;
  106.             //Console.WriteLine("Введите числа");
  107.             //index = int.Parse(Console.ReadLine());
  108.             //index1 = int.Parse(Console.ReadLine());
  109.             LinkedList<T> rez = new LinkedList<T>();
  110.             Node<T> temp = a.head;
  111.             while(temp!=null)
  112.             {
  113.                 if (counter>=1 && counter<=2)
  114.                 {
  115.                     rez = rez + temp.Data;
  116.                    
  117.                 }
  118.                 temp = temp.Next;
  119.                 counter++;
  120.             }
  121.             return rez;
  122.         }
  123.         public static bool operator != (LinkedList<T> a, LinkedList<T> a1)
  124.         {
  125.             if (a.count != a1.count)
  126.                 return true;
  127.             else return false;
  128.         }
  129.         public static bool operator ==(LinkedList<T> a, LinkedList<T> a1)
  130.         {
  131.             if (a.count == a1.count)
  132.                 return true;
  133.             else return false;
  134.         }
  135.         public static bool operator < (LinkedList<T> a, LinkedList<T> a1)
  136.         {
  137.             if (a.count < a1.count)
  138.                 return true;
  139.             else return false;
  140.         }
  141.         public static bool operator > (LinkedList<T> a, LinkedList<T> a1)
  142.         {
  143.             if (a.count > a1.count)
  144.                 return true;
  145.             else return false;
  146.         }
  147.         public static bool operator >=(LinkedList<T> a, LinkedList<T> a1)
  148.         {
  149.             if (a.count >= a1.count)
  150.                 return true;
  151.             else return false;
  152.         }
  153.         public static bool operator <=(LinkedList<T> a, LinkedList<T> a1)
  154.         {
  155.             if (a.count <= a1.count)
  156.                 return true;
  157.             else return false;
  158.         }
  159.         public static LinkedList<T> operator - (LinkedList<T> a, T data)
  160.         {
  161.             // a.tail.Data
  162.             Node<T> current = a.head;
  163.             Node<T> previous = null;
  164.             while (current != null)
  165.             {
  166.                 if (current.Data.Equals(data))
  167.                 {
  168.                     if (previous != null)
  169.                     {
  170.                         previous.Next = current.Next;
  171.                         if (current.Next == null)
  172.                             a.tail = previous;
  173.                     }
  174.                     else
  175.                     {
  176.                         a.head = a.head.Next;
  177.  
  178.                         if (a.head == null)
  179.                             a.tail = null;
  180.                     }
  181.                 }
  182.                 previous = current;
  183.                 current = current.Next;
  184.             }
  185.             return a;
  186.         }
  187.     }
  188.     class Program
  189.     {
  190.         static void Main(string[] args)
  191.         {
  192.             LinkedList<int> My = new LinkedList<int>();
  193.             LinkedList<int> My1 = new LinkedList<int>();
  194.             LinkedList<int> My3 = new LinkedList<int>();
  195.             My.Add(1);
  196.             My.Add(6);
  197.             My.Add(7);
  198.             My.Add(8);
  199.             My.Add(9);
  200.             My.Add(10);
  201.             My.Add(5);
  202.             My--;
  203.             My--;
  204.             My.Show();
  205.             //My1.Add(6);
  206.             //My1.Add(7);
  207.             //My3 = ~My;
  208.             //My3.Show();
  209.            // if (My != My1) Console.WriteLine("Списки не равны!");
  210.             //else Console.WriteLine("Списки равны!");
  211.             //Console.WriteLine(~My);-
  212.             Console.ReadKey();
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement