Advertisement
Guest User

Class Stack

a guest
Jun 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. namespace p03._01.Stack
  2. {
  3.     using System.Collections;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System;
  7.  
  8.     public class Stack<T> : IEnumerable<T>
  9.     {
  10.         private IList<T> stack;
  11.  
  12.         public Stack()
  13.         {
  14.             this.stack = new List<T>();
  15.         }
  16.  
  17.         public void Push(params T[] arguments)
  18.         {
  19.             foreach (var element in arguments)
  20.             {
  21.                 this.stack.Add(element);
  22.             }
  23.         }
  24.  
  25.         public void Pop()
  26.         {
  27.             if (this.stack.Count == 0)
  28.             {
  29.                 throw new ArgumentException("No elements");
  30.             }
  31.  
  32.             this.stack.RemoveAt(this.stack.Count - 1);
  33.         }
  34.  
  35.         public IEnumerator<T> GetEnumerator()
  36.         {
  37.             for (int i = this.stack.Count - 1; i >= 0; i--)
  38.             {
  39.                 yield return this.stack[i];
  40.             }
  41.         }
  42.  
  43.         IEnumerator IEnumerable.GetEnumerator()
  44.         {
  45.             return this.GetEnumerator();
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement