Simple Queue Program in C Programming
Definition
In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue ?dequeue? function. Every time another object or customer enters the line to wait, they join the end of the line and represent the ?enqueue? function. The queue ?size? function would return the length of the line, and the ?empty? function would return true only if there was nothing in the line.
Simple Queue Program
/* Simple Queue Program in C*/
/* Data Structure Programs,C Array Examples */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 100
int main() {
int item, choice, i;
int arr_queue[MAX_SIZE];
int rear = 0;
int front = 0;
int exit = 1;
printf("\nSimple Queue Example - Array");
do {
printf("\n\n Queue Main Menu");
printf("\n1.Insert \n2.Remove \n3.Display \nOthers to exit");
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (rear == MAX_SIZE)
printf("\n## Queue Reached Max!!");
else {
printf("\nEnter The Value to be Insert : ");
scanf("%d", &item);
printf("\n## Position : %d , Insert Value : %d ", rear + 1, item);
arr_queue[rear++] = item;
}
break;
case 2:
if (front == rear)
printf("\n## Queue is Empty!");
else {
printf("\n## Position : %d , Remove Value : %d ", front, arr_queue[front]);
front++;
}
break;
case 3:
printf("\n## Queue Size : %d ", rear);
for (i = front; i < rear; i++)
printf("\n## Position : %d , Value : %d ", i, arr_queue[i]);
break;
default:
exit = 0;
break;
}
} while (exit);
return 0;
}
Sample Output:
Simple Queue Example - Array
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 100
## Position : 1 , Insert Value : 100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 200
## Position : 2 , Insert Value : 200
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 300
## Position : 3 , Insert Value : 300
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 400
## Position : 4 , Insert Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 4
## Position : 0 , Value : 100
## Position : 1 , Value : 200
## Position : 2 , Value : 300
## Position : 3 , Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 0 , Remove Value : 100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 4
## Position : 1 , Value : 200
## Position : 2 , Value : 300
## Position : 3 , Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 1 , Remove Value : 200
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 4
## Position : 2 , Value : 300
## Position : 3 , Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 1100
## Position : 5 , Insert Value : 1100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 5
## Position : 2 , Value : 300
## Position : 3 , Value : 400
## Position : 4 , Value : 1100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 2 , Remove Value : 300
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 3 , Remove Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 4 , Remove Value : 1100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Queue is Empty!
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 4
------------------
(program exited with code: 0)
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )