Our DSA Lab 4
Our DSA Lab 4
Faculty of Engineering
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.
Pushing an element:
1. Compile and examine the program provided below. Print the output in the space
provided.
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 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 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) {}
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;
class Stack {
private:
Node* topNode;
public:
Stack() : topNode(NULL) {}
~Stack() {
while (topNode != NULL) {
Node* temp = topNode;
topNode = topNode->next;
delete temp;
}
}
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);
}
return 0;
}
Output: