Data Structures & Algorithms: Assignment # 02
Data Structures & Algorithms: Assignment # 02
Total Marks: 05
Obtained Marks:
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.
Q no 1:
Write a program to implement Queue by using the concept of singly linkedlist in c++.
Solution:
Code:
#include<iostream>
class Node {
public:
int key;
Node * next;
Node() {
key = 0;
data = 0;
next = NULL;
Node(int k, int d) {
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
key = k;
data = d;
next = NULL;
};
class Queue
public:
Node *front;
Node *rear;
Queue()
front = NULL;
rear = NULL;
bool isEmpty()
return true;
}
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
else
return false;
bool exist=false;
while(temp!=NULL)
if(temp->key==n->key)
exist=true;
break;
temp=temp->next;
return exist;
{
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
if (isEmpty())
front = n;
rear = n;
else if(checkIfNodeExist(n))
else
rear->next=n;
rear=n;
//top = n;
Node* dequeue()
Node *temp=NULL;
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
if (isEmpty())
return NULL;
else
if(front==rear)
temp=front;
front = NULL;
rear = NULL;
return temp;
else
temp=front;
front = front->next;
return temp;
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
int count()
int count=0;
Node *temp=front;
while(temp!=NULL)
count++;
temp=temp->next;
return count;
void display()
if(isEmpty())
else
cout << "All values in the Queue are :" << endl;
Node *temp=front;
while(temp!=NULL)
{
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
temp=temp->next;
cout<<endl;
};
int main() {
Queue q;
do {
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
switch (option) {
case 0:
break;
case 1:
<<endl;
new_node->key = key;
new_node->data = data;
q.enqueue(new_node);
break;
case 2:
new_node = q.dequeue();
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
<<new_node->data<<")";
delete new_node;
cout<<endl;
break;
case 3:
if (q.isEmpty())
else
break;
case 4:
<<endl;
break;
case 5:
q.display();
break;
case 6:
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
system("cls");
break;
default:
return 0;
Output:
DSA SZABIST-ISB