0% found this document useful (0 votes)
16 views6 pages

Queue

The document contains two C programs for implementing queue data structures: a simple queue and a circular queue. Each program includes functions for enqueueing, dequeueing, and displaying elements, along with a menu-driven interface for user interaction. The simple queue has a fixed size, while the circular queue efficiently utilizes space by wrapping around when the end is reached.
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)
16 views6 pages

Queue

The document contains two C programs for implementing queue data structures: a simple queue and a circular queue. Each program includes functions for enqueueing, dequeueing, and displaying elements, along with a menu-driven interface for user interaction. The simple queue has a fixed size, while the circular queue efficiently utilizes space by wrapping around when the end is reached.
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/ 6

#include <stdio.

h>

#include <conio.h>

#define MAX 5

int queue[MAX];

int front = -1, rear = -1;

void enqueue(int value) {

if (rear == MAX - 1) {

printf("Queue is Full!\n");

} else {

if (front == -1)

front = 0;

rear++;

queue[rear] = value;

printf("%d inserted successfully!\n", value);

void dequeue() {

if (front == -1 || front > rear) {

printf("Queue is Empty!\n");

} else {

printf("%d deleted successfully!\n", queue[front]);

front++;

}
void display() {

int i;

if (front == -1 || front > rear) {

printf("Queue is Empty!\n");

} else {

printf("Queue elements are:\n");

for (i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

void main() {

int choice, value;

clrscr();

while (1) {

printf("\n--- Simple Queue Menu ---\n");

printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

enqueue(value);

break;
case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice! Try again.\n");

---

2. Circular Queue Program (Array-based)

#include <stdio.h>

#include <conio.h>

#define MAX 5

int cqueue[MAX];

int front = -1, rear = -1;

void enqueue(int value) {

if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) {

printf("Circular Queue is Full!\n");


} else {

if (front == -1) { // First element

front = rear = 0;

} else if (rear == MAX - 1) {

rear = 0;

} else {

rear++;

cqueue[rear] = value;

printf("%d inserted successfully!\n", value);

void dequeue() {

if (front == -1) {

printf("Circular Queue is Empty!\n");

} else {

printf("%d deleted successfully!\n", cqueue[front]);

if (front == rear) { // Only one element

front = rear = -1;

} else if (front == MAX - 1) {

front = 0;

} else {

front++;

void display() {
int i;

if (front == -1) {

printf("Circular Queue is Empty!\n");

} else {

printf("Circular Queue elements are:\n");

if (front <= rear) {

for (i = front; i <= rear; i++) {

printf("%d ", cqueue[i]);

} else {

for (i = front; i < MAX; i++) {

printf("%d ", cqueue[i]);

for (i = 0; i <= rear; i++) {

printf("%d ", cqueue[i]);

printf("\n");

void main() {

int choice, value;

clrscr();

while (1) {

printf("\n--- Circular Queue Menu ---\n");

printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice! Try again.\n");

You might also like