C Program to Implement Queue Functions using Arrays and Macros

This is a C Program to Implement Queue Functions Using Arrays and Macros.

Problem Description

This program implements queue operations using arrays and macros.

Problem Solution

1. Use #define function to define the macros.
2. Define separate functions for the operations like insert, delete, display etc.
3. Use switch statement to access these functions.

Program/Source Code

Here is source code of the C Program to Implement Queue Functions Using Arrays and Macros. 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 Queue Functions Using Arrays and Macros
  3.  */
  4. #include <stdio.h>
  5. #include<stdlib.h>
  6.  
  7. /* Macro  Definition */
  8. #define MAX 10
  9. #define EMPTY "QUEUE EMPTY"
  10. #define ISFULL rear >=  MAX - 1
  11. #define FULL "QUEUE FULL"
  12. #define ISEMPTY rear == -1
  13.  
  14. /* Global Variable Declaration */
  15. int queue[MAX], front = 0, rear = -1;
  16.  
  17. /* Fucntion Prototypes */
  18. void insert_rear();
  19. void delete_front();
  20. void display_queue();
  21. void empty_queue();
  22. void front_ele();
  23. int queue_size();
  24. void destroy();
  25.  
  26. void main()
  27. {
  28.     int choice, n, flag = 0;
  29.     char ch;
  30.  
  31.     do
  32.     {
  33.         printf("MENU\n");
  34.         printf("Enter 1 to INSERT an element in the queue\n");
  35.         printf("Enter 2 to DELETE an element in the queue\n");
  36.         printf("Enter 3 to DISPLAY the elements of the queue\n");
  37.         printf("Enter 4 to CHECK if the queue is EMPTY\n");
  38.         printf("Enter 5 to KNOW the FIRST element of the queue\n");
  39.         printf("Enter 6 to KNOW the queue SIZE\n");
  40.         printf("Enter 7 to Destroy the Queue\n");
  41.         printf("Enter 8 to EXIT the program\n");
  42.         printf("Enter your Choice:");
  43.         scanf("%d", &choice);
  44.         switch(choice)
  45.         {
  46.         case 1: 
  47.             insert_rear();
  48.             break;
  49.         case 2: 
  50.             delete_front();
  51.             break;
  52.         case 3: 
  53.             display_queue();
  54.             break;
  55.         case 4: 
  56.             empty_queue();
  57.             break;
  58.         case 5: 
  59.             front_ele();
  60.             break;
  61.         case 6: 
  62.             n = queue_size();
  63.             printf("\nthe queue size is: %d", n);
  64.             break;
  65.         case 7: 
  66.             destroy();
  67.             flag = 1;
  68.             break;
  69.         case 8: 
  70.             exit(0);
  71.             break;
  72.         default: 
  73.             printf("WRONG CHOICE\n");
  74.         }
  75.         printf("\nDo you want to continue:");
  76.         scanf(" %c", &ch);
  77.     } while(ch == 'y' || ch == 'Y');
  78.     if (flag == 0)
  79.     {
  80.         destroy();
  81.     }
  82. }
  83.  
  84. /* Code to Insert the element in Queue */
  85. void insert_rear()
  86. {
  87.     int val;
  88.  
  89.     if (ISFULL)
  90.     {
  91.         printf(FULL);
  92.     }
  93.     else
  94.     {
  95.         printf("\nEnter the value you want to insert in the queue:");
  96.         scanf("%d", &val);
  97.         rear++;
  98.         queue[rear] = val;
  99.         printf("\nElement successfully inserted in the queue");
  100.     }    
  101. }
  102.  
  103. /* Code to Delete the element in Queue */
  104. void delete_front()
  105. {
  106.     if (ISEMPTY)
  107.     {
  108.         printf(EMPTY);
  109.     }
  110.     else
  111.     {
  112.         printf("\nThe deleted element is: %d", queue[front]);
  113.         front++;
  114.     }
  115. }
  116.  
  117. /* Code to Display the Elements of Queue */
  118. void display_queue()
  119. {
  120.     int i;
  121.  
  122.     if (ISEMPTY)
  123.     {
  124.         printf(EMPTY);
  125.     }
  126.     else
  127.     {
  128.         for (i = front;i <= rear;i++)
  129.         {
  130.             printf("%d->", queue[i]);
  131.         }
  132.     }
  133. }
  134.  
  135. /* Code to Check the Queue is Empty or Not */
  136. void empty_queue()
  137. {
  138.     if (ISEMPTY)
  139.     {
  140.         printf(EMPTY);
  141.     }
  142.     else
  143.     {
  144.         printf("\nTHE QUEUE has elements\n");
  145.     }
  146. }
  147.  
  148.  
  149. /* Code to Check the First element of Queue */
  150. void front_ele()
  151. {
  152.     if (ISEMPTY)
  153.     {
  154.         printf(EMPTY);
  155.     }
  156.     else
  157.     {
  158.         printf("The first element of the queue is: %d", queue[front]);
  159.     }
  160. }
  161.  
  162. /* Code to Check the Size of Queue */
  163. int queue_size()
  164. {
  165.     int i = 0, count = 0;
  166.  
  167.     if (ISEMPTY)
  168.     {
  169.         printf(EMPTY);
  170.     }
  171.     else
  172.     {
  173.         for (i = front;i <= rear;i++)
  174.         {
  175.             count++;
  176.         }
  177.     }
  178.     return count;
  179. }    
  180.  
  181. /* Code to destroy the queue */    
  182. void destroy()
  183. {
  184.     int size, i;
  185.  
  186.     if (ISEMPTY)
  187.     {
  188.         printf("EMPTY QUEUE CANNOT BE DESTROYED");
  189.     }
  190.     else
  191.     {
  192.         size = queue_size();
  193.  
  194.         for (i = 0;i < size;i++)
  195.         {    
  196.             front++;
  197.         }
  198.         front = 0;
  199.         rear = -1;
  200.         printf("\n\nQUEUE DESTROYED");
  201.     }
  202. }
Program Explanation

1. Ask the user for the operations like insert, delete, display etc.
2. According to the entered option access the respective functions. Use switch statement to access the functions.
3. Use #define function to define macros as mentioned in the program.
4. In the insert_rear() function, ask the user to enter the number to be inserted and store the value in the variable val and copy this value into the array queue[]. Use the variable rear and front to represent the last and first element of the queue.
5. In the delete_front() function, delete the first element of the array and increment the variable front.
6. In the display_queue() function, print all the elements of the array queue.
7. In the front_ele() function, check for the first element of the array queue[].
8. In the empty_queue() function, check if the array queue[] is empty or not.
9. In the queuesize() function, check the size of the array queue[].
10. In the destroy() function, delete all the elements from the array queue[].

advertisement
advertisement
Runtime Test Cases
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:3
QUEUE EMPTY
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:2
QUEUE EMPTY
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:4
QUEUE EMPTY
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:5
QUEUE EMPTY
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:1
 
Enter the value you want to insert in the queue:67
 
Element successfully inserted in the queue
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:1
 
Enter the value you want to insert in the queue:45
 
Element successfully inserted in the queue
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:3
67->45->
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:6
 
the queue size is: 2
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:5
The first element of the queue is: 67
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:2
 
The deleted element is: 67
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:3
45->
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:7
 
 
QUEUE DESTROYED
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:1
 
Enter the value you want to insert in the queue:45
 
Element successfully inserted in the queue
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:3
45->
Do you want to continue:y
MENU
Enter 1 to INSERT an element in the queue
Enter 2 to DELETE an element in the queue
Enter 3 to DISPLAY the elements of the queue
Enter 4 to CHECK if the queue is EMPTY
Enter 5 to KNOW the FIRST element of the queue
Enter 6 to KNOW the queue SIZE
Enter 7 to Destroy the Queue
Enter 8 to EXIT the program
Enter your Choice:8

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

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.