C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY
C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY
- #include<stdio.h>
- #include<conio.h>
- #define MAX 10
- int queue[MAX], front = –1, rear = –1;
- void Insert_Element();
- void Delete_Element();
- void Display_Queue();
- void Empty_Queue();
- int main()
- {
- int option;
- printf(“>>> c program to implement queue operations <<<“);
- do
- {
- printf(“\n\n 1.Insert an element”);
- printf(“\n 2.Delete an element”);
- printf(“\n 3.Display queue”);
- printf(“\n 4.Empty queue”);
- printf(“\n 5.Exit”);
- printf(“\n Enter your choice: “);
- scanf(“%d”, &option);
- switch (option)
- {
- case 1: Insert_Element();
- break;
- case 2: Delete_Element();
- break;
- case 3: Display_Queue();
- break;
- case 4: Empty_Queue();
- break;
- case 5: return 0; /*program ends*/
- }
- } while (option != 5);
- }
- void Insert_Element()
- {
- int num;
- if (rear < MAX – 1)
- {
- if (front == –1)
- /*when queue is initially empty */
- front = 0;
- printf(“\n Enter the number to be inserted: “);
- scanf(“%d”, &num);
- rear = rear + 1;
- queue[rear] = num;
- }
- else
- {
- printf(“\n Queue OverFlow Occured”);
- }
- }
- void Delete_Element()
- {
- int element;
- if (front == –1 || front > rear)
- {
- printf(“\n Queue Underflow occured.\n“);
- return;
- }
- else
- {
- element = queue[front];
- printf(“\n Element deleted from queue is : %d”, element);
- front = front + 1;
- }
- }
- void Display_Queue()
- {
- int i;
- if (front == –1 || front > rear)
- printf(“\n No elements to display”);
- else
- {
- printf(“\n The queue elements are:\n “);
- for (i = front; i <= rear; i++)
- {
- printf(“\t %d”, queue[i]);
- }
- }
- }
- void Empty_Queue()
- {
- /*Reset queue or Creates Empty queue*/
- front = –1;
- rear = –1;
- printf(“\n New Queue created successfully.”);
- }
Sample output :
>>> c program to implement queue operations <<<
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 1
Enter the number to be inserted: 10
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 1
Enter the number to be inserted: 20
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 1
Enter the number to be inserted: 30
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 2
Element deleted from queue is : 10
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice: 3
The queue elements are:
20 30
1.Insert an element
2.Delete an element
3.Display queue
4.Empty queue
5.Exit
Enter your choice:
@horizon : keep visiting my blogs
Good job! I followed this link searching for the ways to highlight code on blogger. I'm going to try ideone.com
Thanks for sharing