0% found this document useful (0 votes)
23 views10 pages

Lab No 04

The document outlines a lab session for BS Cybersecurity and Digital Forensics students at The Islamia University of Bahawalpur, focusing on stack implementation using linked lists. It includes objectives, basic operations, and several programming tasks related to stack operations, such as pushing, popping, and displaying elements. Sample code and outputs for each task are provided to illustrate the implementation and functionality of stacks.

Uploaded by

alsamadlqp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Lab No 04

The document outlines a lab session for BS Cybersecurity and Digital Forensics students at The Islamia University of Bahawalpur, focusing on stack implementation using linked lists. It includes objectives, basic operations, and several programming tasks related to stack operations, such as pushing, popping, and displaying elements. Sample code and outputs for each task are provided to illustrate the implementation and functionality of stacks.

Uploaded by

alsamadlqp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

The Islamia University of Bahawalpur

Faculty of Engineering
BS Cybersecurity and Digital Forensics 3rd semester
Lab # 4 Stack implementation using linked list
Course: Data Structures and Algorithms Date: 20 ,November 2024
lab
Submitted by: Abi Saloom Bhatti Roll No. 72

Objectives
The purpose of this lab session is to understand the implementation of stack using
linked list with its basic operations.
Introduction
We go for a linked list implementation of a stack rather than an array
implementation because of the run time memory allocation feature of linked lists.
In this implementation we make a head pointer and make it point to the first
created node in the stack. Whenever a new node is pushed into the stack we
make the head pointer point to the new node and the new node to point to the
already existing node that was pointed by the head pointer.
Basic Operation Associated with Stacks:
1) Insert (Push) an item into the stack.
2) Delete (Pop) an item from the stack.
3) Display the items.
Creation of head pointer initially:

Pushing an element:

Pushing a second element:

Figure 1: Representation of Stack

Tasks:
• Compile and examine the program provided below. Print the output in the
space provided.
Program (Stack Implementation using linked list):

#include <iostream>
using namespace std;
class stack
{
public:
int stack_top=-1;
int data;
int stack_size=10;
int info;
stack *next;
stack(int i)
{
info=i;
next=0;
}
stack()
{
next=0;
}
int isempty();
int push(int d);
int pop();
void display_items();
};
stack *head=0, *tail=0;
int stack :: isempty()
{
if(stack_top==-1)
{
return 1;
}
else
{
return 0;
}
}
int stack::push(int d)
{
if (head==0)
{
head=tail=new stack(d);
}
else
{
stack *temp=head;
head=new stack(d);
head->next=temp;
}
return 0;
}
int stack::pop()
{
if(head==tail)
{
delete head;
}
else
{
stack *temp=head;
head=head->next;
delete temp;
}
return 0;
}
void stack::display_items()
{
stack *temp=head;
while(temp!=0)
{
cout<<temp->info<<endl;
temp=temp->next;
}
}
int main()
{
stack s;
s.push(7);
s.push(10);
s.push(15);
s.pop();
s.display_items();
return 0;
}

Output:
10
7

Task 1: Develop a program that store first 5 multiple of 8 in the stack and display
the list of elements.
Code:

#include <iostream>
#include <stack>
int main() {
std::stack<int> multiplesOf8;

for (int i = 1; i <= 5; ++i) {


multiplesOf8.push(i * 8);
}

std::cout << "The first 5 multiples of 8 (stored in stack) are:\n";

while (!multiplesOf8.empty()) {
std::cout << multiplesOf8.top() << std::endl;
multiplesOf8.pop();
}
return 0;
}
Output:

The first 5 multiples of 8 (stored in stack) are:


40
32
24
16
8
Task 2: Develop a program that gives the following information using stack.
• Student name
• Student ID
• CNIC
Code:

#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Struct to hold student information
struct Student {
string name;
string studentID;
string cnic;
};
// Function to input student information
Student getStudentInfo() {
Student student;
cout << "Enter Student Name: ";
getline(cin, student.name); // using getline to allow spaces in the name
cout << "Enter Student ID: ";
getline(cin, student.studentID);
cout << "Enter CNIC: ";
getline(cin, student.cnic);
return student;
}
// Function to display student information
void displayStudentInfo(const Student& student) {
cout << "Student Name: " << student.name << endl;
cout << "Student ID: " << student.studentID << endl;
cout << "CNIC: " << student.cnic << endl;
}
int main() {
stack<Student> studentStack;
int choice;
while (true) {
// Display menu
cout << "\nStudent Information Stack Menu:" << endl;
cout << "1. Push student information onto the stack" << endl;
cout << "2. Pop student information from the stack" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // to ignore the trailing newline after the choice input
switch (choice) {
case 1: {
// Push new student info onto the stack
Student student = getStudentInfo();
studentStack.push(student);
cout << "Student information has been pushed to the stack." << endl;
break;
}
case 2: {
// Pop student info from the stack and display it
if (!studentStack.empty()) {
Student student = studentStack.top();
studentStack.pop();
cout << "\nStudent information popped from the stack:" << endl;
displayStudentInfo(student);
} else {
cout << "Stack is empty. No student information to pop." << endl;
}
break;
}
case 3:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
Output:

Student Information Stack Menu:


1. Push student information onto the stack
2. Pop student information from the stack
3. Exit
Enter your choice: 1
Enter Student Name: John Doe
Enter Student ID: 12345
Enter CNIC: 12345-6789012-3
Student information has been pushed to the stack.
Student Information Stack Menu:
1. Push student information onto the stack
2. Pop student information from the stack
3. Exit
Enter your choice: 2
Student information popped from the stack:
Student Name: John Doe
Student ID: 12345
CNIC: 12345-6789012-3

Task 3: Develop a program that store at least 5 elements and then display “No
more space in Stack the stack” (overflow condition) on inserting 6 number
element.
Code:
#include <iostream>
using namespace std;
class Stack {
private:
int stack[5]; // Array to hold stack elements
int top; // Top pointer to the stack
int maxSize; // Maximum size of the stack
public:
// Constructor to initialize stack
Stack() {
top = -1; // Stack is initially empty
maxSize = 5; // Set stack size limit to 5
}
// Function to check if the stack is full
bool isFull() {
return top == maxSize - 1; // Stack is full if top is at maxSize-1
}
// Function to check if the stack is empty
bool isEmpty() {
return top == -1; // Stack is empty if top is -1
}
// Function to push an element onto the stack
void push(int element) {
if (isFull()) {
cout << "No more space in Stack, the stack is full." << endl;
} else {
stack[++top] = element; // Increment top and add element to stack
cout << "Element " << element << " pushed to stack." << endl;
}
}
// Function to pop an element from the stack
void pop() {
if (isEmpty()) {
cout << "Stack is empty, cannot pop." << endl;
} else {
cout << "Element " << stack[top--] << " popped from stack." << endl; //
Decrement top and return the element
}
}
// Function to display the elements of the stack
void display() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack s;

for (int i = 1; i <= 5; i++) {


s.push(i);
}

s.display();

s.push(6);
return 0;
}

Output:
Element 1 pushed to stack.
Element 2 pushed to stack.
Element 3 pushed to stack.
Element 4 pushed to stack.
Element 5 pushed to stack.
Stack elements: 1 2 3 4 5
No more space in Stack, the stack is full.

Task 4: Develop a program which first displays the element which we are going to
pop and then pop it.
Code:

#include <iostream>
using namespace std;
class Stack {
private:
int stack[5]; // Array to hold stack elements (size 5)
int top; // Top pointer to the stack
int maxSize; // Maximum size of the stack
public:
// Constructor to initialize stack
Stack() {
top = -1; // Stack is initially empty
maxSize = 5; // Set stack size limit to 5
}
// Function to check if the stack is full
bool isFull() {
return top == maxSize - 1; // Stack is full if top is at maxSize-1
}
// Function to check if the stack is empty
bool isEmpty() {
return top == -1; // Stack is empty if top is -1
}
// Function to push an element onto the stack
void push(int element) {
if (isFull()) {
cout << "No more space in Stack, the stack is full." << endl;
} else {
stack[++top] = element; // Increment top and add element to stack
cout << "Element " << element << " pushed to stack." << endl;
}
}
// Function to pop an element from the stack
void pop() {
if (isEmpty()) {
cout << "Stack is empty, cannot pop." << endl;
} else {
cout << "Element " << stack[top] << " is going to be popped." << endl;
cout << "Element " << stack[top--] << " popped from stack." << endl; //
Display and pop the top element
}
}
// Function to display the elements of the stack
void display() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack s; // Create a stack object
// Pushing elements into the stack
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
// Display the current stack elements
s.display();
// Pop elements one by one and display the element before popping
s.pop();
s.pop();
s.pop();
// Display the current stack elements after popping
s.display();
// Try popping from an empty stack
s.pop(); // Should show an error message as the stack will be empty after 5
pops
return 0;
}
Output:

Element 10 pushed to stack.


Element 20 pushed to stack.
Element 30 pushed to stack.
Element 40 pushed to stack.
Element 50 pushed to stack.
Stack elements: 10 20 30 40 50
Element 50 is going to be popped.
Element 50 popped from stack.
Element 40 is going to be popped.
Element 40 popped from stack.
Element 30 is going to be popped.
Element 30 popped from stack.
Stack elements: 10 20
Element 20 is going to be popped.
Element 20 popped from stack.

=== Code Execution Successful ===

You might also like