LAB MANUAL
Subject: Data Structures & Algorithms
Department of Computer Science
Federal Urdu university of arts, Science and technology,
Islamabad
Task # 01
Program: Write an algorithm of push() & pop() operation in stack and also
write the algorithm of ispeek(), isfull(), isempty() stack implementation.
#include <iostream>
using namespace std;
// Define a constant for the maximum size of the stack
#define MAX_SIZE 100
// Define a Stack class
class Stack {
private:
int top; // Index of the top element
int stack[MAX_SIZE]; // Array to store stack elements
public:
// Constructor to initialize the stack
Stack() {
top = -1; // Initialize top to -1 (empty stack)
}
// Function to push an item onto the stack
void push(int item) {
// Check for stack overflow
if (top == MAX_SIZE - 1) {
cout << "Stack overflow!" << endl;
return;
}
// Increment top and add item to the stack
stack[++top] = item;
}
// Function to pop an item from the stack
int pop() {
// Check for stack underflow
if (top == -1) {
cout << "Stack underflow!" << endl;
return -1; // Return -1 to indicate error
Page 2 of 7
}
// Decrement top and return the popped item
return stack[top--];
}
// Function to peek at the top item
int peek() {
// Check if the stack is empty
if (top == -1) {
cout << "Stack is empty!" << endl;
return -1; // Return -1 to indicate error
}
// Return the top item without removing it
return stack[top];
}
// Function to check if the stack is empty
bool isempty() {
return top == -1;
}
// Function to print the stack
void printStack() {
// Check if the stack is empty
if (top == -1) {
cout << "Stack is empty!" << endl;
return;
}
// Print the stack from top to bottom
cout << "Stack (top to bottom):" << endl;
for (int i = top; i >= 0; i--) {
cout << stack[i] << endl;
}
}
};
// Main function
int main() {
// Create a Stack object
Stack stack;
Page 3 of 7
// Push items onto the stack
stack.push(5);
stack.push(10);
stack.push(20);
stack.push(30);
// Peek at the top item
cout << "Peek: " << stack.peek() << endl;
// Pop two items from the stack
stack.pop();
stack.pop();
// Print the remaining stack
stack.printStack();
return 0;
}
Explanation:
1. Header Inclusion and Namespace
#include <iostream>
using namespace std;
Includes the iostream header for input/output operations and brings the standard
library namespace into scope.
2. Constant Definition
#define MAX_SIZE 100
Defines a constant MAX_SIZE to specify the maximum size of the stack.
3. Stack Class Definition
class Stack {
private:
int top;
int stack[MAX_SIZE];
Page 4 of 7
public:
// ...
};
Defines a Stack class with private members top (index of the top element) and
stack (array to store elements). Public member functions provide operations on the
stack.
4. Constructor
Stack() {
top = -1;
}
Initializes the stack by setting top to -1, indicating an empty stack.
5. Push Operation
void push(int item) {
if (top == MAX_SIZE - 1) {
cout << "Stack overflow!" << endl;
return;
}
stack[++top] = item;
}
Adds an item to the stack:
Checks for stack overflow (top reaches MAX_SIZE).
Increments top and stores the item at the new top index.
6. Pop Operation
int pop() {
if (top == -1) {
cout << "Stack underflow!" << endl;
return -1;
}
return stack[top--];
}
Removes an item from the stack:
Checks for stack underflow (top is -1).
Returns the top item and decrements top.
Page 5 of 7
7. Peek Operation
int peek() {
if (top == -1) {
cout << "Stack is empty!" << endl;
return -1;
}
return stack[top];
}
Returns the top item without removing it:
Checks if the stack is empty.
Returns the top item.
8. IsEmpty Operation
bool isempty() {
return top == -1;
}
Checks if the stack is empty
9. Print Stack Operation
void printStack() {
if (top == -1) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack (top to bottom):" << endl;
for (int i = top; i >= 0; i--) {
cout << stack[i] << endl;
}
}
Prints the stack from top to bottom:
Checks if the stack is empty.
Iterates from the top index to 0, printing each element.
Page 6 of 7
10. Main Function
int main() {
// ...
}
Demonstrates the usage of the Stack class:
11. Create Stack Object
Stack stack;
Creates a Stack object.
12. Push Items
stack.push(5);
stack.push(10);
stack.push(20);
stack.push(30);
Adds four items to the stack.
13. Peek at Top Item
cout << "Peek: " << stack.peek() << endl;
Prints the top item (30).
14. Pop Two Items
C++
stack.pop();
stack.pop();
Removes the top two items (30 and 20).
15. Print Remaining Stack
stack.printStack();
Prints the remaining stack (10 and 5).
Example Output:
Peek: 30
Stack (top to bottom):
10
5
Page 7 of 7