Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack stk = new Stack(100);
- int rounds = Convert.ToInt32(Console.ReadLine());
- for (int i = 1; i <= rounds; i++)
- {
- string[] str = Console.ReadLine().Split(' ');
- switch (str[0])
- {
- case "push":
- stk.push(Convert.ToInt32(str[1]));
- Console.WriteLine("stack pushed key : " + str[1]);
- break;
- case "pop":
- if (!stk.IsEmpty())
- {
- Console.WriteLine(stk.pop());
- }
- else
- {
- Console.WriteLine("-1");
- }
- break;
- case "size":
- Console.WriteLine(stk.Size());
- break;
- case "top":
- if (!stk.IsEmpty())
- {
- Console.WriteLine(stk.peek());
- }
- else
- {
- Console.WriteLine("-1");
- }
- break;
- case "empty":
- if (!stk.IsEmpty())
- {
- Console.WriteLine("0");
- }
- else
- {
- Console.WriteLine("1");
- }
- break;
- }
- }
- }
- }
- class Stack
- {
- int top, size;
- int[] stack;
- public Stack(int size)
- {
- top = 0;
- stack = new int[size];
- this.size = size;
- }
- public int peek()
- {
- return stack[top];
- }
- public void push(int value)
- {
- stack[++top] = value;
- }
- public int pop()
- {
- return stack[top--];
- }
- public bool IsEmpty()
- {
- if(top > 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public int Size()
- {
- return top;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement