Advertisement
AskTomorrow

Untitled

Feb 14th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 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 Task1
  8. {
  9.     /*
  10.      * class MyStack<T> : IMyStack<T>
  11.     {
  12.         private T[] inner;
  13.  
  14.         int size;
  15.  
  16.         public MyStack()
  17.         {
  18.             Inner = new T[10];
  19.         }
  20.  
  21.         public MyStack(int length)
  22.         {
  23.             Inner = new T[length];
  24.         }
  25.  
  26.         public T[] Inner { get => inner; set => inner = value; }
  27.  
  28.         public T Peek()
  29.         {
  30.             if (!isEmpty)
  31.             {
  32.                 return this.Inner[size - 1];
  33.             }
  34.             else
  35.             {
  36.                 throw new InvalidOperationException();
  37.             }
  38.         }
  39.  
  40.         public T Pop()
  41.         {
  42.             if (!isEmpty)
  43.             {
  44.                 T curr = this.Inner[--size];
  45.                 this.Inner[size] = default(T);
  46.                 //size--;
  47.                 return curr;
  48.             }
  49.             else
  50.             {
  51.                 throw new InvalidOperationException();
  52.             }
  53.         }
  54.  
  55.         public void Push(T item)
  56.         {
  57.             if (Inner.Length == size)
  58.             {
  59.                 this.Resize();
  60.             }
  61.  
  62.             this.Inner[size++] = item;
  63.         }
  64.  
  65.         void Resize()
  66.         {
  67.             T[] newList = new T[size * 2];
  68.             Array.Copy(this.Inner, newList, size);
  69.  
  70.             this.Inner = newList;
  71.         }
  72.  
  73.         public bool isEmpty
  74.         {
  75.             get { return size == 0; }
  76.         }
  77.     }
  78.     */
  79.  
  80.     public class Node<T>
  81.     {
  82.         T data;
  83.  
  84.         public Node(T item)
  85.         {
  86.             this.Data = item;
  87.         }
  88.  
  89.         public T Data { get => data; set => data = value; }
  90.  
  91.         public Node<T> nextNode { get; set; }
  92.     }
  93.  
  94.     public class MyStack<T> : IMyStack<T>
  95.     {
  96.         Node<T> head;
  97.         int size;
  98.  
  99.         T IMyStack<T>.Peek()
  100.         {
  101.             if (!isEmpty)
  102.             {
  103.                 return head.Data;
  104.             }
  105.             else
  106.             {
  107.                 throw new InvalidOperationException();
  108.             }
  109.         }
  110.  
  111.         T IMyStack<T>.Pop()
  112.         {
  113.             if (!isEmpty)
  114.             {
  115.                 Node<T> currNode = head;
  116.                 head = head.nextNode;
  117.                 size--;
  118.                 return currNode.Data;
  119.             }
  120.             else
  121.             {
  122.                 throw new InvalidOperationException();
  123.             }
  124.         }
  125.  
  126.         void IMyStack<T>.Push(T item)
  127.         {
  128.             size++;
  129.             Node<T> newNode = new Node<T>(item);
  130.             newNode.nextNode = head;
  131.             head = newNode;
  132.         }
  133.  
  134.         public bool isEmpty
  135.         {
  136.             get { return size == 0; }
  137.         }
  138.  
  139.     }
  140.  
  141.     class Program
  142.     {
  143.         static void Main(string[] args)
  144.         {
  145.             MyStack<int> myStack = new MyStack<int>();
  146.             Test(myStack);
  147.             Console.ReadKey();
  148.         }
  149.  
  150.         static void Test(IMyStack<int> stack)
  151.         {
  152.             foreach (var i in Enumerable.Range(0, 9))
  153.             {
  154.                 stack.Push(i);
  155.             }
  156.             Console.WriteLine(stack.Pop());
  157.             Console.WriteLine(stack.Peek());
  158.             foreach (int i in Enumerable.Repeat(2, 5))
  159.             {
  160.                 stack.Push(i);
  161.             }
  162.             try
  163.             {
  164.                 while (true) { Console.WriteLine(stack.Pop()); }
  165.             }
  166.             catch (InvalidOperationException)
  167.             {
  168.                 Console.WriteLine("Stack is empty");
  169.             }
  170.         }
  171.     }
  172.  
  173.     interface IMyStack<T>
  174.     {
  175.         void Push(T item);
  176.         T Pop();
  177.         T Peek();
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement