If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# Program to Implement Stack with Push and Pop operations with the output.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, I have shared the output program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { stack st = new stack(); while (true) { Console.Clear(); Console.WriteLine("\nStack MENU(size -- 10)"); Console.WriteLine("1. Add an element"); Console.WriteLine("2. See the Top element."); Console.WriteLine("3. Remove top element."); Console.WriteLine("4. Display stack elements."); Console.WriteLine("5. Exit"); Console.Write("Select your choice: "); int choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter an Element : "); st.Push(Console.ReadLine()); break; case 2: Console.WriteLine("Top element is: {0}", st.Peek()); break; case 3: Console.WriteLine("Element removed: {0}", st.Pop()); break; case 4: st.Display(); break; case 5: System.Environment.Exit(1); break; } Console.ReadKey(); } } } interface StackADT { Boolean isEmpty(); void Push(Object element); Object Pop(); Object Peek(); void Display(); } class stack : StackADT { private int StackSize; public int StackSizeSet { get { return StackSize; } set { StackSize = value; } } public int top; Object[] item; public stack() { StackSizeSet = 10; item = new Object[StackSizeSet]; top = -1; } public stack(int capacity) { StackSizeSet = capacity; item = new Object[StackSizeSet]; top = -1; } public bool isEmpty() { if (top == -1) return true; return false; } public void Push(object element) { if (top == (StackSize - 1)) { Console.WriteLine("Stack is full!"); } else { item[++top] = element; Console.WriteLine("Item pushed successfully!"); } } public object Pop() { if (isEmpty()) { Console.WriteLine("Stack is empty!"); return "No elements"; } else { return item[top--]; } } public object Peek() { if (isEmpty()) { Console.WriteLine("Stack is empty!"); return "No elements"; } else { return item[top]; } } public void Display() { for (int i = top; i > -1; i--) { Console.WriteLine("Item {0}: {1}", (i + 1), item[i]); } } } } |
Stack MENU(size — 10)
1. Add an element
2. See the Top Element
3. Remove the Top Element
4. Display Stack Elements
5. Exit
Select your Choice : 1
Enter the Element : 25
Item Pushed Successfully!
Select your choice :1
Enter the Element : 26
Item Pushed Successfully!
Select your choice : 4
Item 2 :26
Item 1 :25
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.