C Program to Implement Queue using Array

This is a C Program to Implement a queue using array.

Problem Description

This C program implements the queue operations using array.

Problem Solution

1. Use three functions for three operations like insert, delete and display.
2. Use switch statement to access these functions.
3. Exit.

Program/Source Code

Here is source code of the C Program to implement a queue using array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement a Queue using an Array
  3.  */
  4. #include <stdio.h>
  5.  
  6. #define MAX 50
  7.  
  8. void insert();
  9. void delete();
  10. void display();
  11. int queue_array[MAX];
  12. int rear = - 1;
  13. int front = - 1;
  14. main()
  15. {
  16.     int choice;
  17.     while (1)
  18.     {
  19.         printf("1.Insert element to queue \n");
  20.         printf("2.Delete element from queue \n");
  21.         printf("3.Display all elements of queue \n");
  22.         printf("4.Quit \n");
  23.         printf("Enter your choice : ");
  24.         scanf("%d", &choice);
  25.         switch (choice)
  26.         {
  27.             case 1:
  28.             insert();
  29.             break;
  30.             case 2:
  31.             delete();
  32.             break;
  33.             case 3:
  34.             display();
  35.             break;
  36.             case 4:
  37.             exit(1);
  38.             default:
  39.             printf("Wrong choice \n");
  40.         } /* End of switch */
  41.     } /* End of while */
  42. } /* End of main() */
  43.  
  44. void insert()
  45. {
  46.     int add_item;
  47.     if (rear == MAX - 1)
  48.     printf("Queue Overflow \n");
  49.     else
  50.     {
  51.         if (front == - 1)
  52.         /*If queue is initially empty */
  53.         front = 0;
  54.         printf("Inset the element in queue : ");
  55.         scanf("%d", &add_item);
  56.         rear = rear + 1;
  57.         queue_array[rear] = add_item;
  58.     }
  59. } /* End of insert() */
  60.  
  61. void delete()
  62. {
  63.     if (front == - 1 || front > rear)
  64.     {
  65.         printf("Queue Underflow \n");
  66.         return ;
  67.     }
  68.     else
  69.     {
  70.         printf("Element deleted from queue is : %d\n", queue_array[front]);
  71.         front = front + 1;
  72.     }
  73. } /* End of delete() */
  74.  
  75. void display()
  76. {
  77.     int i;
  78.     if (front == - 1)
  79.         printf("Queue is empty \n");
  80.     else
  81.     {
  82.         printf("Queue is : \n");
  83.         for (i = front; i <= rear; i++)
  84.             printf("%d ", queue_array[i]);
  85.         printf("\n");
  86.     }
  87. } /* End of display() */
Program Explanation

1. Ask the user for the operation like insert, delete, display and exit.
2. According to the option entered, access its respective function using switch statement. Use the variables front and rear to represent the first and last element of the queue.
3. In the function insert(), firstly check if the queue is full. If it is, then print the output as “Queue Overflow”. Otherwise take the number to be inserted as input and store it in the variable add_item. Copy the variable add_item to the array queue_array[] and increment the variable rear by 1.
4. In the function delete(), firstly check if the queue is empty. If it is, then print the output as “Queue Underflow”. Otherwise print the first element of the array queue_array[] and decrement the variable front by 1.
5. In the function display(), using for loop print all the elements of the array starting from front to rear.
6. Exit.

advertisement
advertisement
Runtime Test Cases
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 20
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
15 20 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.