Advertisement
Timtsa

Steck

Apr 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 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 ConsoleApp6
  8. {
  9.     class Program
  10.     {
  11. class Stack
  12.         {
  13.             int capacity;
  14.             int count;
  15.             bool isAmpty;
  16.             bool isFull;
  17.             int[] arr;
  18.            
  19.             public int Capacity
  20.             {
  21.              get { return capacity; }
  22.             }
  23.  
  24.             public int Count
  25.             {
  26.                 get { return count; }
  27.             }
  28.             public bool IsAmpty
  29.             {
  30.                get { return isAmpty; }
  31.             }
  32.  
  33.             public bool IsFull
  34.             {
  35.                 get { return isFull; }
  36.             }
  37.  
  38.  
  39.  
  40.             public  Stack( int _capacity)
  41.             {
  42.                 capacity = _capacity;
  43.                 arr = new int[capacity];
  44.                 isAmpty = true;
  45.                 isFull = false;
  46.                 count = 0;
  47.             }
  48.  
  49.           public  int Pop ()
  50.             {
  51.                 int temp = arr[count-1];
  52.                 count--;
  53.                 arr[count] = 0;
  54.                 isFull = false;
  55.                 if (count<1)
  56.                 {
  57.                     isAmpty = true;
  58.                 }
  59.                 return temp;
  60.  
  61.             }
  62.           public  int Peek ()
  63.             {
  64.                
  65.                 return arr[count-1];
  66.                
  67.                    
  68.             }
  69.  
  70.             public void Push(int value)
  71.             {
  72.                 try {
  73.                 arr[count] = value;
  74.                 ++count;
  75.                 isAmpty = false;
  76.                 if (count == capacity - 1)
  77.                 {
  78.                     isFull = true;
  79.                 }
  80.            
  81.            
  82.                 }
  83.                
  84.  
  85.         }
  86.  
  87.         static void Main(string[] args)
  88.         {
  89.  
  90.             Stack stack = new Stack(5);
  91.             stack.Push(3);
  92.             stack.Push(2);
  93.             Console.WriteLine( stack.Pop());
  94.          
  95.             Console.WriteLine(stack.Count);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement