Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace laba6._3
- {
- public class Node<T>
- {
- public T Data { get; set; }
- public Node(T data)
- {
- Data = data;
- }
- public Node<T> Next { get; set; }
- }
- public class LinkedList<T>
- {
- public Node<T> head; // головной/первый элемент
- Node<T> tail; // последний/хвостовой элемент //pnext
- int count; // количество элементов в списке
- // добавление элемента
- public int s()
- {
- return count;
- }
- public void Add(T data)
- {
- Node<T> node = new Node<T>(data);
- if (head == null)
- head = node;
- else
- tail.Next = node;
- tail = node;
- count++;
- }
- public void Show()
- {
- Node<T> temp = this.head;
- while(temp.Next!=null)
- {
- temp = temp.Next;
- Console.WriteLine(temp.Data);
- }
- }
- public static LinkedList<T> operator -- (LinkedList<T> a)
- {
- Node<T> previos = a.head;
- int k;
- k = a.count;
- for (int i = 0; i < (k-1)-1; i++)
- {
- previos = previos.Next;
- }
- Node<T> toDelete = previos.Next;
- previos.Next = toDelete.Next;
- toDelete=null;
- return a;
- }
- public static LinkedList<T> operator + (LinkedList<T> a, T k)
- {
- if (a.head==null)
- {
- a.head = new Node<T>(k);
- }
- Node<T> current = a.head;
- while(current.Next!=null)
- {
- current = current.Next;
- }
- current.Next = new Node<T>(k);
- return a;
- }
- public static LinkedList<T> operator + (LinkedList<T> a, LinkedList<T> a2)
- {
- Node<T> current = a.head;
- Node<T> temp = a2.head;
- while (current.Next != null)
- current = current.Next;
- current.Next = new Node<T>(a2.head.Data);
- current = current.Next;
- while(temp.Next!=null)
- {
- current.Next = new Node<T>(temp.Next.Data);
- temp = temp.Next;
- current = current.Next;
- }
- return a;
- }
- public static LinkedList<T> operator ~ (LinkedList<T> a)
- {
- int counter = 0;
- // int index, index1;
- //Console.WriteLine("Введите числа");
- //index = int.Parse(Console.ReadLine());
- //index1 = int.Parse(Console.ReadLine());
- LinkedList<T> rez = new LinkedList<T>();
- Node<T> temp = a.head;
- while(temp!=null)
- {
- if (counter>=1 && counter<=2)
- {
- rez = rez + temp.Data;
- }
- temp = temp.Next;
- counter++;
- }
- return rez;
- }
- public static bool operator != (LinkedList<T> a, LinkedList<T> a1)
- {
- if (a.count != a1.count)
- return true;
- else return false;
- }
- public static bool operator ==(LinkedList<T> a, LinkedList<T> a1)
- {
- if (a.count == a1.count)
- return true;
- else return false;
- }
- public static bool operator < (LinkedList<T> a, LinkedList<T> a1)
- {
- if (a.count < a1.count)
- return true;
- else return false;
- }
- public static bool operator > (LinkedList<T> a, LinkedList<T> a1)
- {
- if (a.count > a1.count)
- return true;
- else return false;
- }
- public static bool operator >=(LinkedList<T> a, LinkedList<T> a1)
- {
- if (a.count >= a1.count)
- return true;
- else return false;
- }
- public static bool operator <=(LinkedList<T> a, LinkedList<T> a1)
- {
- if (a.count <= a1.count)
- return true;
- else return false;
- }
- public static LinkedList<T> operator - (LinkedList<T> a, T data)
- {
- // a.tail.Data
- Node<T> current = a.head;
- Node<T> previous = null;
- while (current != null)
- {
- if (current.Data.Equals(data))
- {
- if (previous != null)
- {
- previous.Next = current.Next;
- if (current.Next == null)
- a.tail = previous;
- }
- else
- {
- a.head = a.head.Next;
- if (a.head == null)
- a.tail = null;
- }
- }
- previous = current;
- current = current.Next;
- }
- return a;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- LinkedList<int> My = new LinkedList<int>();
- LinkedList<int> My1 = new LinkedList<int>();
- LinkedList<int> My3 = new LinkedList<int>();
- My.Add(1);
- My.Add(6);
- My.Add(7);
- My.Add(8);
- My.Add(9);
- My.Add(10);
- My.Add(5);
- My--;
- My--;
- My.Show();
- //My1.Add(6);
- //My1.Add(7);
- //My3 = ~My;
- //My3.Show();
- // if (My != My1) Console.WriteLine("Списки не равны!");
- //else Console.WriteLine("Списки равны!");
- //Console.WriteLine(~My);-
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement