1.1 What is a Stack?
A Stack (Abstract Data Type - ADT) is a linear data structure that follows the
principle:
LIFO (Last In, First Out)
This means:
The element inserted last is removed first.
Real-Life Examples
Stack of plates
Undo operation in editors
Function call stack in programming
1.2 Basic Operations of Stack
Operation Description
Push (x) Insert element at top
pop Remove top element
Peek()/top() View top element
isEmpty() Check if stack is empty
IsFull() (Array case) Check overflow
1.3 Stack ADT Definition
Stack is an ADT because:
It defines what operations are performed
Not how they are implemented
2. Array Implementation of Stack
2.1 Concept
Stack is implemented using an array with a variable:
top → index of the top element
Initial Condition:
top = -1
, 2.2 Operations
Push Operation
top = top + 1
stack[top] = value
Pop Operation
value = stack[top]
top = top - 1
2.3 C++ Program (Array Stack)
Question Statement
Write a C++ program to implement stack using array with push, pop, and
display operations.
#include <iostream> // Input/Output ke liye library
using namespace std; // std namespace use karne ke liye
#define SIZE 5 // Stack ka maximum size define kiya (5 elements)
class Stack {
int arr[SIZE]; // Array jo stack ko store karega
int top; // 'top' variable stack ke last element ka index batata hai
public:
// Constructor: jab object banay ga tab yeh automatically call hoga
Stack() {
top = -1; // Initially stack empty hota hai is liye top = -1
}
// Push function: element insert karne ke liye
void push(int x) {
// Check agar stack full ho gaya hai
if (top == SIZE - 1) {