0% found this document useful (0 votes)
29 views9 pages

Our DSA Lab 4

The document describes implementing a stack using a linked list. It discusses the basic operations of a stack like push, pop and display. It then provides tasks to develop programs using stacks to store multiples of 8, student details and display overflow condition.

Uploaded by

Ummi
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)
29 views9 pages

Our DSA Lab 4

The document describes implementing a stack using a linked list. It discusses the basic operations of a stack like push, pop and display. It then provides tasks to develop programs using stacks to store multiples of 8, student details and display overflow condition.

Uploaded by

Ummi
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/ 9

The Islamia University of Bahawalpur

Faculty of Engineering

BS Cyber security and Digital Forensics 3rd semester


Lab # 4 Stack implementation using linked list
Course: Data Structures and Algorithms lab Date:
Submitted by: Roll No.

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:

1. 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;
}
Output:

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

Code:

#include <iostream>
using namespace std;

class MultiplesOfEightStack {
int elements[5];
int top;

public:
MultiplesOfEightStack() : top(-1) {}

void push(int element) {


if (top >= 4) {
cout << "Stack full. Cannot add more elements." << endl;
} else {
elements[++top] = element;
}
}

void displayElements() {
for (int i = top; i >= 0; i--) {
cout << elements[i] << endl;
}
}
};

int main() {
MultiplesOfEightStack stack;
for (int i = 1; i <= 5; i++) {
stack.push(i * 8);
}
stack.displayElements();

return 0;
}

Output:
Task 2: Develop a program that gives the following information using stack.

⮚ Student name

⮚ Student ID

⮚ CNIC

Code:

#include <iostream>
using namespace std;

struct Student {
string name;
string studentID;
string CNIC;
};

class StudentStack {
Student students[5];
int top;

public:
StudentStack() : top(-1) {}

void push(Student student) {


if (top >= 4) {
cout << "Stack is full. Cannot add more students." << endl;
} else {
students[++top] = student;
}
}

void displayAll() {
for (int i = top; i >= 0; i--) {
cout << "Student Name: " << students[i].name << ", ID: " << students[i].studentID << ", CNIC: "
<< students[i].CNIC << endl;
}
}
};

int main() {
StudentStack stack;
stack.push({"Ahmad", "S234026", "346026543876-1"});
stack.push({"Ali", "S234001", "346026543876-2"});
stack.push({"Abdullah", "S234054", "346026543876-3"});
stack.push({"Talha", "S234045", "346026543876-4"});
stack.push({"Raza", "S234034", "346026543876-5"});
stack.displayAll();
return 0;
}

Output:

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 {
int elements[5];
int top;

public:
Stack() : top(-1) {}

void push(int element) {


if (top >= 4) {
cout << "Oops! No more space in Stack. ??" << endl;
} else {
elements[++top] = element;
cout << "Element " << element << " added to the stack." << endl;
}
}
};

int main() {
Stack myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
myStack.push(6);
return 0;
}
Output:

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 Node {
public:
int data;
Node* next;

Node(int val) : data(val), next(NULL) {} // Initialize next to NULL here


};

class Stack {
private:
Node* topNode;

public:
Stack() : topNode(NULL) {}

~Stack() {
while (topNode != NULL) {
Node* temp = topNode;
topNode = topNode->next;
delete temp;
}
}

// Push element onto the stack


void push(int val) {
Node* newNode = new Node(val);
newNode->next = topNode;
topNode = newNode;
}

// Pop element from the stack


void pop() {
if (!isEmpty()) {
int topElement = topNode->data;
cout << "Element to be popped: " << topElement << endl;
Node* temp = topNode;
topNode = topNode->next;
delete temp;
} else {
cout << "Stack is empty. Cannot pop." << endl;
}
}

// Check if the stack is empty


bool isEmpty() {
return topNode == NULL;
}
};

int main() {
Stack myStack;
int value;

cout << "Enter 5 elements to push onto the stack:" << endl;
for (int i = 0; i < 5; ++i) {
cin >> value;
myStack.push(value);
}

cout << "Stack elements before popping: ";


while (!myStack.isEmpty()) {
myStack.pop();
}

return 0;
}

Output:

You might also like