Professional Documents
Culture Documents
Dsaaaaaok
Dsaaaaaok
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;
};
if (temp == NULL) {
head = newNode;
}
%
Enrollment Number: ____________________________
else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
}
cout << " NULL ";
}
else {
Queue* temp = head;
head = head->next;
free(temp);
class Stack
{
public:
Stack* next;
int data;
Stack(int data) {
this->data = data;
next = NULL;
};
}
int poppedItem = top->data;
Stack* temp = top;
top = top->next;
delete temp;
return poppedItem;
Page 2 of 4
%
Enrollment Number: ____________________________
}
}
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.
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 4 of 4