Thu Jun 28 2018
Queue
C Programming1163 views
File Name: queue.c
#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;
}
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");
while(node != NULL) {
printf("%d\n", node->data);
node = node->next;
}
}
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!
Reference:
Author:Geekboots