You are on page 1of 5

University of Engineering and Technology Peshawar

Data Structures and Algorithms


Assignment 3

Submitted by:
Muhammad Maaz Khan
19PWBCS0687
Section B
3rd semester

Submitted to:
Dr. Iftikhar Ahmad
1) Improvement in C++ code of Queue using % operator
const int size = 10;
struct myQueue {
private:
int head = -1;
int tail = -1;
int QUEUE[size];

public:
bool isEmpty() {
if (head == -1 && tail == -1)
return true;
return false;
}
bool isFull() {
if ((tail + 1) % size == head)
return true;
return false;
}
//Enqueue
void enQueue(int value) {
if (isFull()) {
cout << "QUEUE is full" << endl;
return;
}
else if (isEmpty()) {
tail = 0;
head = 0;
QUEUE[tail] = value;

}
else {
tail = (tail + 1) % size;
QUEUE[tail] = value;
}
}
//Dequeue
int deQueue() {
if (isEmpty()) {
return -999;
}

else if (tail == head) {


int item = QUEUE[tail];
tail = -1;
head = -1;
return item;
}

else {
int item = QUEUE[head];
QUEUE[head] = 0;
head = (head + 1) % size;
return item;
}
}
};

2) Queues using Linked Lists:

struct Node {
int data;
Node *next;
}
*head = NULL,*tail = NULL,*currentPtr = NULL,*nextPtr = NULL;

void enQueue (int value) {

nextPtr = new Node;


nextPtr->data = value;
nextPtr->next = NULL;

if (head == NULL) {
head = tail = nextPtr;
tail->next = NULL;
}

else {
tail->next = nextPtr;
tail = nextPtr;
tail->next = NULL;
}
}

int deQueue () {

int value;

if (head == NULL) {
cout << "Queue is empty!!\n";
return -999;
}
else {
currentPtr = head;
value = currentPtr->data;
head = head->next;
delete (currentPtr);
return (value);
}
}

You might also like