Circular Queue

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

/* Queue structure */
struct queue {
	int data;
	struct queue *next;
} *front = NULL, *rear = NULL, *node = NULL;

/* Function for Enqueue data into the Queue */
void enqueue() {

	/* Dynamic memory allocation */
	node = (struct queue*)malloc(sizeof(struct queue));
	printf("Enter value for queue:\n");
	scanf("%d", &node->data);
	node->next = NULL;
	if(front == NULL)
		front = rear = node;
	else {
		front->next = node;
		front = node;
		front->next = rear;
	}
	printf("Data Enqueue into Queue successfully!\n");					
}

/* Function for Dequeue data from Queue */
void dequeue() {
	if(rear != NULL) {
		node = rear;
		printf("Value %d will Dequeue from Queue!\n", node->data);
		rear = node->next;
		free(node);
	}
	else
		printf("Queue is Empty!\n");
}
										
/* Function for Display data of the Queue */
void display() {
	node = rear;
	printf("List of data into the Queue:\n");
	printf("%d\n", node->data);
	while(node != front) {
		node = node->next;
		printf("%d\n", node->data);										
	}					
}

int main() {
	int op = 0;
	while(op != 4) {
		printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
		printf("Enter your choice:\n");
		scanf("%d", &op);
		switch(op) {
			case 1:
				enqueue();
				break;
			case 2:
				dequeue();
				break;
			case 3:
				display();
				break;
			case 4:
				printf("Bye Bye!\n");
				exit(0);
				break;
			default:
				printf("Invalid choice!\n");
		}
	}
	return 0;
}




/* Output */
1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice:
1

Enter value for Queue:
5

Data Enqueue into Queue successfully!

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice:
1

Enter value for Queue:
6

Data Enqueue into Queue successfully!

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice:
1

Enter value for Queue:
7

Data Enqueue into Queue successfully!

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice:
2

Value 5 will Dequeue from the Queue!

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice:
3

List of data into the Queue:
6
7

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice:
4

Bye Bye!

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!