0% found this document useful (0 votes)
4 views5 pages

Circular Queue using arrays

The document contains two implementations of a circular queue in C. The first implementation includes functions for insertion, deletion, and display, while the second is a more concise version with similar functionalities. Both implementations handle queue overflow and underflow conditions.

Uploaded by

TR Masir Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Circular Queue using arrays

The document contains two implementations of a circular queue in C. The first implementation includes functions for insertion, deletion, and display, while the second is a more concise version with similar functionalities. Both implementations handle queue overflow and underflow conditions.

Uploaded by

TR Masir Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

void insert(int value) {


if ((front == 0 && rear == SIZE - 1) || (rear == (front - 1) % (SIZE - 1)))
{
printf("Queue is Full\n");
return;
}
else if (front == -1) {
front = rear = 0;
}
else if (rear == SIZE - 1 && front != 0) {
rear = 0;
} else {
rear++;
}
queue[rear] = value;
printf("Inserted %d\n", value);
}

void delete() {
if (front == -1)
{
printf("Queue is Empty\n");
return;
}

printf("Deleted %d\n", queue[front]);

if (front == rear) {
front = rear = -1;
} else if (front == SIZE - 1) {
front = 0;
} else {
front++;
}
}

void display() {
int i;
if (front == -1) {
printf("Queue is Empty\n");
return;
}

printf("Queue: ");
if (rear >= front) {
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
} else {
for (i = front; i < SIZE; i++)
printf("%d ", queue[i]);
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
printf("\n");
}

void main() {
int choice, value;
do {
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid Choice\n");
}
} while (choice != 4);
}

2 . new and short pg

#include <stdio.h>
#define MAX_SIZE 5

int queue[MAX_SIZE];
int front = -1, rear = -1;

int isFull()
{

return (rear + 1) % MAX_SIZE == front;


}

int isEmpty()
{

return front == -1;


}

void enqueue(int data)


{

if (isFull()) {
printf("Queue overflow\n");
return;
}

if (front == -1) {
front = 0;
}

rear = (rear + 1) % MAX_SIZE;


queue[rear] = data;
printf("Element %d inserted\n", data);
}
int dequeue()
{
// return -1
if (isEmpty()) {
printf("Queue underflow\n");
return -1;
}

int data = queue[front];

if (front == rear) {
front = rear = -1;
}
else {

front = (front + 1) % MAX_SIZE;


}

return data;
}

void display()
{

if (isEmpty()) {
printf("Queue is empty\n");
return;
}

printf("Queue elements: ");


int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
}

printf("%d\n", queue[rear]);
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
display();

printf("Dequeued element: %d\n", dequeue());

display();
return 0;
}

You might also like