0% found this document useful (0 votes)
17 views

Data Structures & Algorithms: Assignment # 02

The document is an assignment sheet from the Computer Science Department of Shaheed Zulfikar Ali Bhutto Institute of Science & Technology. It contains instructions for Assignment #02 on Data Structures and Algorithms, with a due date of August 16, 2023. It includes one question to implement a queue using a singly linked list in C++, along with sample code that defines Node and Queue classes to perform enqueue, dequeue and other queue operations.

Uploaded by

Sarwar Bh
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)
17 views

Data Structures & Algorithms: Assignment # 02

The document is an assignment sheet from the Computer Science Department of Shaheed Zulfikar Ali Bhutto Institute of Science & Technology. It contains instructions for Assignment #02 on Data Structures and Algorithms, with a due date of August 16, 2023. It includes one question to implement a queue using a singly linked list in C++, along with sample code that defines Node and Queue classes to perform enqueue, dequeue and other queue operations.

Uploaded by

Sarwar Bh
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/ 11

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 05

Obtained Marks:

Data Structures &


Algorithms
Assignment # 02
Last date of Submission: 16th Aug, 2023

Submitted To: Mr. Muneeb Muzammil


_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name: Ghulam Sarwar


______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg. Number: 2012212


______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

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>

using namespace std;

class Node {

public:

int key;

int data; // value

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

COMPUTER SCIENCE DEPARTMENT

key = k;

data = d;

next = NULL;

};

class Queue

public:

Node *front;

Node *rear;

Queue()

front = NULL;

rear = NULL;

bool isEmpty()

if(front==NULL && rear==NULL)

return true;

}
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

else

return false;

bool checkIfNodeExist(Node *n)

Node *temp = front;

bool exist=false;

while(temp!=NULL)

if(temp->key==n->key)

exist=true;

break;

temp=temp->next;

return exist;

void enqueue(Node *n)

{
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

if (isEmpty())

front = n;

rear = n;

cout<<"Node ENQUEUED successfully"<<endl;

else if(checkIfNodeExist(n))

cout<<"Node already exist with this Key value."

<<"Enter different Key value"<<endl;

else

rear->next=n;

rear=n;

//top = n;

cout<<"Node ENQUEUED successfully"<<endl;

Node* dequeue()

Node *temp=NULL;
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

if (isEmpty())

cout << "Queue is Empty" << endl;

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

COMPUTER SCIENCE DEPARTMENT

int count()

int count=0;

Node *temp=front;

while(temp!=NULL)

count++;

temp=temp->next;

return count;

void display()

if(isEmpty())

cout << "Queue is Empty" << endl;

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

COMPUTER SCIENCE DEPARTMENT

cout<<"("<<temp->key<<","<<temp->data<<")"<<" -> ";

temp=temp->next;

cout<<endl;

};

int main() {

Queue q;

int option,key, data;

do {

cout << "What operation do you want to perform?"

<<"Select Option number. Enter 0 to exit." << endl;

cout << "1. Enqueue()" << endl;

cout << "2. Dequeue()" << endl;

cout << "3. isEmpty()" << endl;

cout << "4. count()" << endl;

cout << "5. display()" << endl;

cout << "6. Clear Screen" << endl << endl;

DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

cin >> option;

//Node n1 = new Node();

Node * new_node = new Node();

switch (option) {

case 0:

break;

case 1:

cout << "ENQUEUE Function Called -" <<endl;

cout << "Enter KEY and VALUE of NODE to ENQUEUE "

<<"in the Queue"

<<endl;

cin >> key;

cin >> data;

new_node->key = key;

new_node->data = data;

q.enqueue(new_node);

break;

case 2:

cout << "DEQUEUE Function Called - " <<endl;

new_node = q.dequeue();

cout<<"Dequeued Value is: ("<<new_node->key<<","

DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

<<new_node->data<<")";

delete new_node;

cout<<endl;

break;

case 3:

cout << "isEmpty Function Called - " << endl;

if (q.isEmpty())

cout << "Queue is Empty" << endl;

else

cout << "Queue is NOT Empty" << endl;

break;

case 4:

cout << "Count Function Called - " << endl;

cout << "No of nodes in the Queue: " <<q.count()

<<endl;

break;

case 5:

cout << "Display Function Called - " << endl;

q.display();

cout << endl;

break;

case 6:
DSA SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

system("cls");

break;

default:

cout << "Enter Proper Option number " << endl;

} while (option != 0);

return 0;

Output:

DSA SZABIST-ISB

You might also like