C Program to Implement Queue Functions using Dynamic Memory Allocation

This is a C Program to Implement various Queue Functions using Dynamic Memory Allocation.

Problem Description

This program implements queue operations using dynamic memory allocation.

Problem Solution

1. Use malloc function to allocate memory.
2. Define separate functions for the operations like insert, delete and display.
3. Use switch statement to access these functions.

Program/Source Code

Here is source code of the C Program to Implement various Queue Functions using Dynamic Memory Allocation. 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 various Queue Functions using Dynamic Memory Allocation
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define MAX 10
  7.  
  8. struct node
  9. {
  10.     int data;
  11.     struct node *link;
  12. }*front, *rear;
  13.  
  14. // function protypes
  15. void insert();
  16. void delete();
  17. void queue_size();
  18. void check();
  19. void first_element();
  20.  
  21. void main()
  22. {
  23.     int choice, value;
  24.  
  25.     while(1)
  26.     {
  27.         printf("enter the choice \n");
  28.         printf("1 : create an empty queue \n2 : Insert element\n");
  29.         printf("3 : Dequeue an element \n4 : Check if empty\n");
  30.         printf("5. Get the first element of the queue\n");
  31.         printf("6. Get the number of entries in the queue\n");
  32.         printf("7. Exit\n");
  33.         scanf("%d", &choice);
  34.         switch (choice)    // menu driven program
  35.         {
  36.         case 1: 
  37.             printf("Empty queue is created with a capacity of %d\n", MAX);
  38.             break;
  39.         case 2:    
  40.             insert();
  41.             break;
  42.         case 3: 
  43.             delete();
  44.             break;
  45.         case 4: 
  46.             check();
  47.             break;
  48.         case 5: 
  49.             first_element();
  50.             break;
  51.         case 6: 
  52.             queue_size();
  53.             break;
  54.         case 7: 
  55.             exit(0);
  56.         default: 
  57.             printf("wrong choice\n");
  58.             break;
  59.         }
  60.     }
  61. }
  62.  
  63. // to insert elements in queue
  64. void insert()
  65. {
  66.     struct node *temp;
  67.  
  68.     temp = (struct node*)malloc(sizeof(struct node));
  69.     printf("Enter value to be inserted \n");
  70.     scanf("%d", &temp->data);
  71.     temp->link = NULL;
  72.     if (rear  ==  NULL)
  73.     {
  74.         front = rear = temp;
  75.     }
  76.     else
  77.     {
  78.         rear->link = temp;
  79.         rear = temp;
  80.     }    
  81. }
  82.  
  83. // delete elements from queue
  84. void delete()
  85. {
  86.     struct node *temp;
  87.  
  88.     temp = front;
  89.     if (front == NULL)
  90.     {
  91.         printf("queue is empty \n");
  92.         front = rear = NULL;
  93.     }
  94.     else
  95.     {    
  96.         printf("deleted element is %d\n", front->data);
  97.         front = front->link;
  98.         free(temp);
  99.     }
  100. }
  101.  
  102. // check if queue is empty or not
  103. void check()
  104. {
  105.     if (front == NULL)
  106.         printf("\nQueue is empty\n");
  107.     else
  108.         printf("*************** Elements are present in the queue **************\n");
  109. }
  110.  
  111. // returns first element of queue
  112. void first_element()
  113. {
  114.     if (front == NULL)
  115.     {
  116.         printf("**************** The queue is empty ****************\n");
  117.     }
  118.     else
  119.         printf("**************** The front element is %d ***********\n", front->data);        
  120. }
  121.  
  122. // returns number of entries and displays the elements in queue
  123. void queue_size()
  124. {
  125.     struct node *temp;
  126.  
  127.     temp = front;
  128.     int cnt = 0;
  129.     if (front  ==  NULL)
  130.     {
  131.         printf(" queue empty \n");
  132.     }
  133.     while (temp)
  134.     {
  135.         printf("%d  ", temp->data);
  136.         temp = temp->link;
  137.         cnt++;
  138.     }
  139.     printf("********* size of queue is %d ******** \n", cnt);
  140. }
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 structure with a data and a pointer as the data module. Use malloc function to assign the memory dynamically.
4. In the insert() function ask the user to enter the number to be inserted and store the value into the new data module’s data.
5. In the delete() function delete the element at the front.
6. In the check() function, check if the queue is empty or not.
7. In the first_element() function, print the first element of the queue.

advertisement
advertisement
Runtime Test Cases
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
6
 
**************Size is 0 ************
 
enter your choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
3
queue is empty
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
4
queue is empty
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
5
****************The queue is empty****************
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
2
enter value to insert
45
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
2
enter value to insert
56
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
2
enter value to insert
67
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
2
enter value to insert
78
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
2
enter value to insert
89
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
6
- 45 -- 56 -- 67 -- 78 -- 89 -
**************Size is 5 ************
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
5
****************The front element is 45 ***********
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
3
******45 has been removed******
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
3
******56 has been removed******
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
3
******67 has been removed******
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
6
- 78 -- 89 -
**************Size is 2 ************
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
3
******78 has been removed******
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
3
******89 has been removed******
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
6
 
**************Size is 0 ************
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
2
enter value to insert
34
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
6
- 34 -
**************Size is 1 ************
 
enter the choice
1 : create an empty queue
2 : Insert element
3 : Dequeue an element
4 : Check if empty
5 : Get the first element of the queue
6 : Get the number of entries in the queue
7 : Exit
7

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.