Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Name: Shaikh Inamul Hasan

Roll No: 100

Assignment:6
Circular Queue:
A circular queue is a data structure that extends the functionality of a regular queue using an
array or a linked list. It follows the First-In-First-Out (FIFO) principle, but with the added
feature that once the last position in the underlying array is reached, elements wrap around to
the beginning, forming a circular structure. This allows for efficient use of space and
continuous cycling of elements. Circular queues are commonly used in situations where a fixed
amount of memory is allocated, such as managing data in a printer's buffer, keyboard input in
an operating system, or any scenario where you need to cycle through a fixed number of
elements efficiently.
Code:
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;
void insertCQ(int val) {
if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1)Insert\n";
cout<<"2)Delete\n";
cout<<"3)Display\n";
cout<<"4)Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
Output:

Priority Queue using Linked list:


A priority queue implemented using a linked list is a data structure that stores elements with
associated priorities. Elements with higher priorities are dequeued before those with lower
priorities. Linked lists provide a dynamic structure to manage the elements and their priorities
efficiently. Each node in the linked list contains both the element and its associated priority.
Operations like insertion and deletion are performed based on the priority value, ensuring that
higher-priority elements are dequeued first. This data structure is useful in various applications,
such as task scheduling, job management, and resource allocation, where prioritization is
essential.
Code:
# include<stdio.h>
# include<malloc.h>
typedef struct node
{
int priority;
int info;
struct node *link;
}NODE;
NODE *front = NULL;
// insert method
void insert(int data,int priority)
{
NODE *temp,*q;
temp = (NODE *)malloc(sizeof(NODE));
temp->info = data;
temp->priority = priority;
if( front == NULL || priority < front->priority )
{
temp->link = front;
front = temp;
}
else
{
q = front;
while( q->link != NULL && q->link->priority <= priority )
q=q->link;
temp->link = q->link;
q->link = temp;
}
}
// delete method
void del()
{
NODE *temp;
// condition to check whether the Queue is empty or not
if(front == NULL)
printf("Queue Underflow\n");
else
{
temp = front;
printf("Deleted item is %d\n", temp->info);
front = front->link;
free(temp);
}}
// display method
void display()
{
NODE *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{
printf("%5d %5d\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}
}
/*End of display*/
// main method
int main()
{
int choice, data, priority;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the data which is to be added in the queue : ");
scanf("%d",&data);
printf("Enter its priority : ");
scanf("%d",&priority);
insert(data,priority);
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
break;
default :
printf("Wrong choice\n");
}
}while(choice!=4);
return 0;
}
Output:

You might also like