m1okgoodyes

Deque

Apr 2nd, 2022
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. namespace Deque
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Deque<string> deque = new Deque<string>();
  12.             deque.AddFirst("alice");
  13.             deque.AddLast("kate");
  14.             deque.AddLast("tom");
  15.             foreach(string item in deque)
  16.             {
  17.                 Console.WriteLine(item);
  18.             }
  19.             string removedItem = deque.RemoveFirst();
  20.             Console.WriteLine($"\n удален: {removedItem}\n");
  21.             foreach(string item in deque)
  22.             {
  23.                 Console.WriteLine(item);
  24.             }
  25.         }
  26.         public class DoublyNode<T>
  27.         {
  28.             public DoublyNode(T data)
  29.             {
  30.                 Data = data;
  31.             }
  32.             public T Data { get; set; }
  33.             public DoublyNode<T> Next { get; set; }
  34.             public DoublyNode<T> Previous { get; set; }
  35.         }
  36.         public class Deque<T> : IEnumerable<T>//двухсязный список
  37.         {
  38.             DoublyNode<T> head;
  39.             DoublyNode<T> tail;
  40.             int count;
  41.  
  42.             //добавление элемента
  43.             public void AddLast(T data)
  44.             {
  45.                 DoublyNode<T> node = new DoublyNode<T>(data);
  46.                 if(head == null)
  47.                 {
  48.                     head = node;
  49.                 }
  50.                 else
  51.                 {
  52.                     tail.Next = node;
  53.                     node.Previous = tail;
  54.                 }
  55.                 tail = node;
  56.                 count++;
  57.             }
  58.             public void AddFirst(T data)
  59.             {
  60.                 DoublyNode<T> node = new DoublyNode<T>(data);
  61.                 DoublyNode<T> temp = head;
  62.                 node.Next = temp;
  63.                 head = node;
  64.                 if(count == 0)
  65.                 {
  66.                     tail = head;
  67.                 }
  68.                 else
  69.                 {
  70.                     temp.Previous = node;
  71.                 }
  72.                 count++;
  73.             }
  74.             public T RemoveFirst()
  75.             {
  76.                 if(count == 0)
  77.                 {
  78.                     throw new InvalidOperationException();
  79.                 }
  80.                 T output = head.Data;
  81.                 if(count == 1)
  82.                 {
  83.                     head = tail = null;
  84.                 }
  85.                 else
  86.                 {
  87.                     head = head.Next;
  88.                     head.Previous = null;
  89.                 }
  90.                 count--;
  91.                 return output;
  92.             }
  93.             public T RemoveLast()
  94.             {
  95.                 if(count == 0)
  96.                 {
  97.                     throw new InvalidOperationException ();
  98.                 }
  99.                 T output = tail.Data;
  100.                 if (count == 1)
  101.                 {
  102.                     head = tail = null;
  103.                 }
  104.                 else
  105.                 {
  106.                     tail = tail.Previous;
  107.                     tail.Next = null;
  108.                 }
  109.                 count--;
  110.                 return output;
  111.             }
  112.             public T First
  113.             {
  114.                 get
  115.                 {
  116.                     if(IsEmpty)
  117.                     {
  118.                         throw new InvalidOperationException();
  119.                     }
  120.                     return tail.Data;
  121.                 }
  122.             }
  123.             public int Count { get { return count; } }
  124.             public bool IsEmpty { get { return count == 0; } }
  125.             public void Clear()
  126.             {
  127.                 head = tail = null;
  128.                 count = 0;
  129.             }
  130.             public bool Contains (T data)
  131.             {
  132.                 DoublyNode<T> current = head;
  133.                 while(current != null)
  134.                 {
  135.                     if(current.Data.Equals(data))
  136.                     {
  137.                         return true;
  138.                     }
  139.                     current = current.Next;
  140.                 }
  141.                 return false;
  142.             }
  143.             IEnumerator IEnumerable.GetEnumerator()
  144.             {
  145.                 return ((IEnumerable)this).GetEnumerator();
  146.             }
  147.             IEnumerator<T> IEnumerable<T>.GetEnumerator()
  148.             {
  149.                 DoublyNode<T> current = head;
  150.                 while(current != null)
  151.                 {
  152.                     yield return current.Data;
  153.                     current = current.Next;
  154.                 }
  155.             }
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment