Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 04
(Fall 2023)

Course: Data Structures and Algorithm - Lab Date: _______________


Course Code: CSL-221 Max Marks: 10
Faculty’s Name: Fatima Zulfiqar

Name: _____________________ Enroll No: ___________________ Class:


______________

Objective(s):
Upon completion of this lab session, learners will be able to:
• Implement stack and queue data structure using linked list.
• Use an appropriate data structure to implement the given scenario-based problem.

Lab Tasks:

Task 1
Implement stack and queue data structure using linked list and perform following operations.
Your program should contain all checks required to incorporate the properties of a stack and
queue.
 enqueue ()
 dequeue ()
 push ()
 pop ()
 display ()

#include <iostream>
using namespace std;

class Queue {
public:
Queue* next;
int data;
Queue(int data) {
this->data = data;
next = NULL;

};

void enque(Queue*& head, int data) {


Queue* temp = head;
Queue* newNode = new Queue(data);

if (temp == NULL) {
head = newNode;
}
%
Enrollment Number: ____________________________
else {
while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
}
}

void display(Queue* head) {


Queue* temp = head;
while (temp != NULL) {
cout << temp->data;
temp = temp->next;
cout << " <-> ";

}
cout << " NULL ";

void dequeue(Queue*& head) {


if (head == NULL) {
cout << "Queue is empty, cannot delete" << endl;

}
else {
Queue* temp = head;
head = head->next;
free(temp);

class Stack
{
public:
Stack* next;
int data;

Stack(int data) {
this->data = data;
next = NULL;

};

void push(Stack*& top, int data)


{
Stack* newNode = new Stack(data);
newNode->next = top;
top = newNode;
}

int pop(Stack*& top)


{
if (top == NULL)
{
cout << "Stack is empty, cannot pop" << endl;

}
int poppedItem = top->data;
Stack* temp = top;
top = top->next;
delete temp;
return poppedItem;
Page 2 of 4
%
Enrollment Number: ____________________________
}

void stackDisplay(Stack* top)


{
Stack* temp = top;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;

}
cout << "NULL" << endl;
}
int main() {
Queue* head = NULL;
Stack*top = NULL;

enque(head, 5);
enque(head, 7);
enque(head, 8);
display(head);
cout << endl;

dequeue(head);
display(head);
cout << endl;
cout << "---------------------" << endl;;
cout << endl;

push(top, 5);
push(top, 7);
push(top, 9);
cout << "\nStack:" << endl;
stackDisplay(top);
cout << "Popped: " << pop(top) << endl;
cout << "Stack after popping:" << endl;
stackDisplay(top);

system("pause");
return 0;

Task 2
Suppose you are working in a hospital as a nurse, and you are responsible for managing a
queue of patients waiting at the reception area to see a doctor. Consider the following
scenario.

At the start of your shift there are 6 patients waiting to see a doctor. The first patient in the
queue has minor injury and can wait for up to 30 minutes to see the doctor. The second
patient has severe illness and need to be seen by the doctor immediately. The remaining four
patient have moderate injuries and can wait up to 15 minutes.

Identify the type of data structure that will be most appropriate to implement the above
scenario. Implement the scenario with a choice of your data structure. What is the order in
which a patient will be seen by the doctor? Also, print the order in which the doctor will first
see a patient along with their illness.

Note:

Page 3 of 4
%
Enrollment Number: ____________________________
You are required to implement the above scenario using a linked list. Your program
should contain all the helping functions needed to solve the problem. In the main
function demonstrate the above scenario.

Lab Grading Sheet :


Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 03
2. 07
Total 10 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Page 4 of 4

You might also like