You are on page 1of 14

ASSIGNMENT

CIRCULAR QUEUE

#include<bits/stdc++.h>

using namespace std;

class Node{

public:

int val;

Node* next;

Node(int v):val(v),next(nullptr){}

};

class CircularQueue{

Node *front,*rear;

void enqueueHelper(int val){

Node* node=new Node(val);

if(!node){

cout<<"Insertion Unsuccessful\n";

return;

if(front==nullptr)

front=node;

else

rear->next=node;

rear=node;
rear->next=front;

cout<<val<<" Inserted Successfully\n";

void dequeueHelper(){

if(front==nullptr){

cout<<"Queue is Empty\n";

return;

if(front==rear){

Node* node=front;

front=rear=nullptr;

delete node;

else{

Node* node=front;

front=front->next;

rear->next=front;

delete node;

cout<<"Node Deleted Successfully\n";

int peekHelper(){

return front==nullptr?-1:front->val;

void displayHelper(){

Node* node=front;

do{

cout<<node->val<<" ";

node=node->next;
}while(node!=front);

cout<<"\n";

public:

CircularQueue():front(nullptr),rear(nullptr){}

void enqueue(){

int val;

cout<<"Enter Value to Insert : ";

cin>>val;

enqueueHelper(val);

void dequeue(){

dequeueHelper();

void peek(){

int val=peekHelper();

if(val==-1){

cout<<"Queue is Empty\n";

return;

cout<<"Front Value is : "<<val<<"\n";

void display(){

if(front==nullptr){

cout<<"Queue is Empty\n";

return;

cout<<"Queue is : ";

displayHelper();
}

};

void displayMenu(){

cout<<"\n";

cout << "1. Create Queue\n";

cout << "2. Insert\n";

cout << "3. Delete\n";

cout << "4. Peek\n";

cout << "5. Display\n";

cout << "6. Exit\n";

cout << "Enter your choice : ";

int main(){

CircularQueue* cq=nullptr;

int choice;

do{

displayMenu();

cin>>choice;

switch(choice){

case 1 : cq=new CircularQueue();

cout<<"Circular Queue Created Successfully\n";

break;

case 2 : cq->enqueue();

break;

case 3 : cq->dequeue();

break;
case 4 : cq->peek();

break;

case 5 : cq->display();

break;

case 6 : break;

default: cout<<"Wrong Choice Selected\n";

}while(choice!=6);

return 0;

}
PRIORITY QUEUE

#include<bits/stdc++.h>

using namespace std;

class Node{

public:

int val,priority;

Node* next;

Node(int v,int p):val(v),priority(p),next(nullptr){}

};

class PriorityQueue{

Node *front;

void enqueueHelper(int val,int priority){

Node* node=new Node(val,priority);

if(!node){

cout<<"Insertion Unsuccessful\n";

return;

if(front==nullptr)

front=node;

else{

if(front->priority<priority){

node->next=front;

front=node;

else{
Node* temp=front;

while(temp->next&&temp->next->priority>priority){

temp=temp->next;

node->next=temp->next;

temp->next=node;

cout<<val<<" Inserted Successfully\n";

void dequeueHelper(){

if(front==nullptr){

cout<<"Queue is Empty\n";

return;

Node* node=front;

front=front->next;

delete node;

cout<<"Node Deleted Successfully\n";

int peekHelper(){

return front==nullptr?-1:front->val;

void displayHelper(){

Node* node=front;

while(node){

cout<<"("<<node->val<<", "<<node->priority<<") ";

node=node->next;

}
cout<<"\n";

public:

PriorityQueue():front(nullptr){}

void enqueue(){

int val;

cout<<"Enter Value to Insert : ";

cin>>val;

int priority;

cout<<"Enter Priority of Node : ";

cin>>priority;

enqueueHelper(val,priority);

void dequeue(){

dequeueHelper();

void peek(){

int val=peekHelper();

if(val==-1){

cout<<"Queue is Empty\n";

return;

cout<<"Front Value is : "<<val<<"\n";

void display(){

if(front==nullptr){

cout<<"Queue is Empty\n";

return;

}
cout<<"Queue is : ";

displayHelper();

};

void displayMenu(){

cout<<"\n";

cout << "1. Create Queue\n";

cout << "2. Insert\n";

cout << "3. Delete\n";

cout << "4. Peek\n";

cout << "5. Display\n";

cout << "6. Exit\n";

cout << "Enter your choice : ";

int main(){

PriorityQueue* pq=nullptr;

int choice;

do{

displayMenu();

cin>>choice;

switch(choice){

case 1 : pq=new PriorityQueue();

cout<<"Priority Queue Created Successfully\n";

break;

case 2 : pq->enqueue();

break;
case 3 : pq->dequeue();

break;

case 4 : pq->peek();

break;

case 5 : pq->display();

break;

case 6 : break;

default: cout<<"Wrong Choice Selected\n";

}while(choice!=6);

return 0;

You might also like