This is a C Program to Implement various Queue Functions using Dynamic Memory Allocation.
This program implements queue operations using dynamic memory allocation.
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.
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.
/*
* C Program to Implement various Queue Functions using Dynamic Memory Allocation
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct node
{
int data;
struct node *link;
}*front, *rear;
// function protypes
void insert();
void delete();
void queue_size();
void check();
void first_element();
void main()
{
int choice, value;
while(1)
{
printf("enter the choice \n");
printf("1 : create an empty queue \n2 : Insert element\n");
printf("3 : Dequeue an element \n4 : Check if empty\n");
printf("5. Get the first element of the queue\n");
printf("6. Get the number of entries in the queue\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) // menu driven program
{
case 1:
printf("Empty queue is created with a capacity of %d\n", MAX);
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
check();
break;
case 5:
first_element();
break;
case 6:
queue_size();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
break;
}
}
}
// to insert elements in queue
void insert()
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter value to be inserted \n");
scanf("%d", &temp->data);
temp->link = NULL;
if (rear == NULL)
{
front = rear = temp;
}
else
{
rear->link = temp;
rear = temp;
}
}
// delete elements from queue
void delete()
{
struct node *temp;
temp = front;
if (front == NULL)
{
printf("queue is empty \n");
front = rear = NULL;
}
else
{
printf("deleted element is %d\n", front->data);
front = front->link;
free(temp);
}
}
// check if queue is empty or not
void check()
{
if (front == NULL)
printf("\nQueue is empty\n");
else
printf("*************** Elements are present in the queue **************\n");
}
// returns first element of queue
void first_element()
{
if (front == NULL)
{
printf("**************** The queue is empty ****************\n");
}
else
printf("**************** The front element is %d ***********\n", front->data);
}
// returns number of entries and displays the elements in queue
void queue_size()
{
struct node *temp;
temp = front;
int cnt = 0;
if (front == NULL)
{
printf(" queue empty \n");
}
while (temp)
{
printf("%d ", temp->data);
temp = temp->link;
cnt++;
}
printf("********* size of queue is %d ******** \n", cnt);
}
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.
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
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Data Structure Internship
- Apply for Information Technology Internship
- Practice Programming MCQs
- Buy Programming Books
- Buy Computer Science Books