Lab 3
Lab 3
Lab 3
Objective:
The objective of this lab is to help computer science students understand the concept of a stack
and its operations. This lab will use a basic array-based implementation of a stack in C++,
focusing on the following key stack operations:
Prerequisites:
Instructions
1. Introduction to Stacks: A stack is a data structure that follows the LIFO principle,
where elements are added (pushed) and removed (popped) from the top. Think of it like
stacking books; you can only access the book at the top of the stack.
2. Array-based Stack: We'll use an array to simulate a stack where the index position
represents the position of elements in the stack. We need a variable ( top) to keep track of
the topmost element.
Step-by-step Implementation:
#include <iostream>
using namespace std;
class Stack {
private:
int arr[MAX]; // Array to store stack elements
int top; // To keep track of the top element
1|Page
public:
// Constructor to initialize the stack
Stack() {
top = -1; // Stack is initially empty
}
int main() {
Stack stack; // Create a stack object
2|Page
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
return 0;
}
1. Class Definition:
o The class Stack encapsulates the stack operations and an array (arr) to store the
elements. The top variable keeps track of the last element pushed onto the stack.
If top is -1, it indicates that the stack is empty.
2. Stack Operations:
o push(): Adds an element to the stack if it is not full. It first checks if the stack is
full using the isFull() method. If not, it increments the top and adds the
element to the array.
o pop(): Removes the top element if the stack is not empty. The function
decrements the top index after printing the popped value.
o peek(): Returns the top element without removing it. It uses isEmpty() to check
if the stack is empty before trying to return the top value.
3. Stack Overflow and Underflow:
o If you attempt to push an element when the stack is full, the program will print a
"Stack Overflow" message.
o If you attempt to pop an element when the stack is empty, the program will print a
"Stack Underflow" message.
3|Page
Step 4: Sample Output
Pushed: 10
Pushed: 20
Pushed: 30
Pushed: 40
Pushed: 50
Stack Overflow! Cannot push 60
Top element is: 50
Popped: 50
Popped: 40
Popped: 30
Top element after popping is: 20
Popped: 20
Popped: 10
Stack Underflow! Nothing to pop.
Conclusion:
In this lab, we implemented a stack using an array in C++, focusing on its core operations: push,
pop, and peek. This basic implementation helps students understand how stacks work and lays
the groundwork for more complex stack-based applications in data structures.
4|Page
Data Structures Lab: Linked List Implementation of a Stack in C++
Objective:
The goal of this lab is to demonstrate the Linked List implementation of a Stack and its
operations using C++. This approach provides a dynamic way to manage a stack as opposed to
the fixed-size array-based implementation. We will focus on the following operations:
Prerequisites:
Instructions:
1. Introduction to Stacks: A stack is a data structure that follows the LIFO principle,
where elements are added (pushed) and removed (popped) from the top. The linked list
implementation dynamically allocates memory as needed.
2. Linked List-based Stack: In this implementation, each element in the stack is stored in a
node, and each node points to the next node. A pointer top keeps track of the top node in
the stack.
Step-by-step Implementation:
#include <iostream>
using namespace std;
5|Page
o The Stack class contains the necessary functions to implement the stack
operations using a linked list.
class Stack {
private:
Node* top; // Pointer to the top node in the stack
public:
// Constructor to initialize the stack
Stack() {
top = nullptr; // Initially, the stack is empty
}
int main() {
Stack stack; // Create a stack object
6|Page
// Perform push operations
stack.push(10);
stack.push(20);
stack.push(30);
return 0;
}
1. Node Structure:
o The Node structure defines each element in the stack. It contains two fields: data
to store the element value and next to point to the next node in the stack.
2. Stack Class:
o Push: Adds a new node to the stack. The newNode is created dynamically, its
data is set to the value passed, and it is linked to the previous top node. The new
node becomes the top of the stack.
o Pop: Removes the top node from the stack. It first checks if the stack is empty. If
not, it stores the top node in a temporary variable, moves the top pointer to the
next node, and deletes the old top node.
o Peek: Returns the value at the top of the stack without removing it. It returns -1 if
the stack is empty.
o isEmpty: Checks if the stack is empty by comparing the top pointer to nullptr.
3. Dynamic Memory:
o Since we're using a linked list, memory for each new node is allocated
dynamically using new. When nodes are popped, the memory is freed using
delete.
7|Page
Pushed: 20
Pushed: 30
Top element is: 30
Popped: 30
Popped: 20
Top element after popping is: 10
Popped: 10
Stack is now empty.
Stack Underflow! Nothing to pop.
Conclusion:
In this lab, students learned how to implement a stack using a linked list in C++. This dynamic
approach allows the stack to grow and shrink as needed, without worrying about a fixed size.
The lab focused on core stack operations and memory management in C++.
8|Page