C++ Stack

Introduction

Stack is a data structure that follows the Last In First Out (LIFO) principle.
Stack
Source: Programiz

Implementation with array

Stack
Source: Simplilearn

Stack can be implemented using array. The following are the basic operations:
push: Add an element to the top of the stack.
pop: Remove the top element from the stack.

Pseudocode

Pseudocode for push operation
push(element)
	if stack is full
		return stack overflow
	top = top + 1
	stack[top] = element
Pseudocode for pop operation
pop()
	if stack is empty
		return stack underflow
	element = stack[top]
	top = top - 1
	return element

Task

Create a stack with length 5. Implement the push and pop operations.
Sample output:
1. push
2. pop
3. exit
Enter your choice: 1
Enter element: 10
1. push
2. pop
3. exit
Enter your choice: 1
Enter element: 20
1. push
2. pop
3. exit
Enter your choice: 2
Element popped: 20
1. push
2. pop
3. exit
Enter your choice: 2
Element popped: 10
1. push
2. pop
3. exit
Enter your choice: 3

Reference

Queue ADT