C# Program to Implement Stack Using Array
Push to add, Pop to remove, and Display to show elements; for example, define int[] stack = new int[size] and int top = -1 to start.Examples
How to Think About It
top to keep track of the last added element's position. When you add (push) an item, increase top and store the item there. When you remove (pop) an item, take the item at top and decrease top. Check for overflow when pushing and underflow when popping.Algorithm
Code
using System; class Stack { private int[] stack; private int top; private int size; public Stack(int size) { this.size = size; stack = new int[size]; top = -1; } public void Push(int item) { if (top == size - 1) { Console.WriteLine("Stack Overflow"); return; } stack[++top] = item; Console.WriteLine($"Pushed {item}"); } public void Pop() { if (top == -1) { Console.WriteLine("Stack Underflow"); return; } Console.WriteLine($"Popped {stack[top--]}"); } public void Display() { if (top == -1) { Console.WriteLine("Stack is empty"); return; } Console.Write("Stack elements: "); for (int i = 0; i <= top; i++) { Console.Write(stack[i] + " "); } Console.WriteLine(); } } class Program { static void Main() { Stack s = new Stack(3); s.Push(10); s.Push(20); s.Pop(); s.Display(); } }
Dry Run
Let's trace pushing 10, pushing 20, popping, and displaying the stack.
Initialize stack
stack = [_, _, _], top = -1
Push 10
top increments to 0, stack[0] = 10
Push 20
top increments to 1, stack[1] = 20
Pop
Return stack[1] = 20, top decrements to 0
Display
Print stack elements from 0 to top: 10
| Operation | top | Stack contents |
|---|---|---|
| Initialize | -1 | [] |
| Push 10 | 0 | [10, _, _] |
| Push 20 | 1 | [10, 20, _] |
| Pop | 0 | [10, 20, _] |
| Display | 0 | [10, 20, _] |
Why This Works
Step 1: Using an array and top index
The array stores stack elements, and top tracks the last added element's position.
Step 2: Push operation
Increase top and place the new element there, checking for overflow to avoid errors.
Step 3: Pop operation
Return the element at top and decrease top, checking for underflow to avoid removing from empty stack.
Alternative Approaches
using System; using System.Collections.Generic; class StackList { private List<int> stack = new List<int>(); private int size; public StackList(int size) { this.size = size; } public void Push(int item) { if (stack.Count == size) { Console.WriteLine("Stack Overflow"); return; } stack.Add(item); Console.WriteLine($"Pushed {item}"); } public void Pop() { if (stack.Count == 0) { Console.WriteLine("Stack Underflow"); return; } int item = stack[stack.Count - 1]; stack.RemoveAt(stack.Count - 1); Console.WriteLine($"Popped {item}"); } public void Display() { if (stack.Count == 0) { Console.WriteLine("Stack is empty"); return; } Console.Write("Stack elements: "); foreach (var item in stack) { Console.Write(item + " "); } Console.WriteLine(); } } class Program { static void Main() { StackList s = new StackList(3); s.Push(5); s.Push(15); s.Pop(); s.Display(); } }
using System; class Node { public int data; public Node next; public Node(int data) { this.data = data; next = null; } } class StackLinkedList { private Node top; public StackLinkedList() { top = null; } public void Push(int item) { Node newNode = new Node(item); newNode.next = top; top = newNode; Console.WriteLine($"Pushed {item}"); } public void Pop() { if (top == null) { Console.WriteLine("Stack Underflow"); return; } Console.WriteLine($"Popped {top.data}"); top = top.next; } public void Display() { if (top == null) { Console.WriteLine("Stack is empty"); return; } Console.Write("Stack elements: "); Node current = top; while (current != null) { Console.Write(current.data + " "); current = current.next; } Console.WriteLine(); } } class Program { static void Main() { StackLinkedList s = new StackLinkedList(); s.Push(7); s.Push(14); s.Pop(); s.Display(); } }
Complexity: O(1) time, O(n) space
Time Complexity
Push and pop operations take constant time O(1) because they only update the top index and array element.
Space Complexity
The stack uses O(n) space where n is the size of the array allocated for the stack.
Which Approach is Fastest?
Array-based stack is fastest for fixed size due to direct indexing; linked list is flexible but uses more memory and slightly more overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Array-based Stack | O(1) | O(n) | Fixed size, fast access |
| List | O(1) | O(n) | Dynamic size with some overhead |
| Linked List Stack | O(1) | O(n) | Dynamic size, no fixed limit |