This is a C Program to Implement Queue Functions Using Arrays and Macros.
This program implements queue operations using arrays and macros.
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.
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.
/*
* C Program to Implement Queue Functions Using Arrays and Macros
*/
#include <stdio.h>
#include<stdlib.h>
/* Macro Definition */
#define MAX 10
#define EMPTY "QUEUE EMPTY"
#define ISFULL rear >= MAX - 1
#define FULL "QUEUE FULL"
#define ISEMPTY rear == -1
/* Global Variable Declaration */
int queue[MAX], front = 0, rear = -1;
/* Fucntion Prototypes */
void insert_rear();
void delete_front();
void display_queue();
void empty_queue();
void front_ele();
int queue_size();
void destroy();
void main()
{
int choice, n, flag = 0;
char ch;
do
{
printf("MENU\n");
printf("Enter 1 to INSERT an element in the queue\n");
printf("Enter 2 to DELETE an element in the queue\n");
printf("Enter 3 to DISPLAY the elements of the queue\n");
printf("Enter 4 to CHECK if the queue is EMPTY\n");
printf("Enter 5 to KNOW the FIRST element of the queue\n");
printf("Enter 6 to KNOW the queue SIZE\n");
printf("Enter 7 to Destroy the Queue\n");
printf("Enter 8 to EXIT the program\n");
printf("Enter your Choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert_rear();
break;
case 2:
delete_front();
break;
case 3:
display_queue();
break;
case 4:
empty_queue();
break;
case 5:
front_ele();
break;
case 6:
n = queue_size();
printf("\nthe queue size is: %d", n);
break;
case 7:
destroy();
flag = 1;
break;
case 8:
exit(0);
break;
default:
printf("WRONG CHOICE\n");
}
printf("\nDo you want to continue:");
scanf(" %c", &ch);
} while(ch == 'y' || ch == 'Y');
if (flag == 0)
{
destroy();
}
}
/* Code to Insert the element in Queue */
void insert_rear()
{
int val;
if (ISFULL)
{
printf(FULL);
}
else
{
printf("\nEnter the value you want to insert in the queue:");
scanf("%d", &val);
rear++;
queue[rear] = val;
printf("\nElement successfully inserted in the queue");
}
}
/* Code to Delete the element in Queue */
void delete_front()
{
if (ISEMPTY)
{
printf(EMPTY);
}
else
{
printf("\nThe deleted element is: %d", queue[front]);
front++;
}
}
/* Code to Display the Elements of Queue */
void display_queue()
{
int i;
if (ISEMPTY)
{
printf(EMPTY);
}
else
{
for (i = front;i <= rear;i++)
{
printf("%d->", queue[i]);
}
}
}
/* Code to Check the Queue is Empty or Not */
void empty_queue()
{
if (ISEMPTY)
{
printf(EMPTY);
}
else
{
printf("\nTHE QUEUE has elements\n");
}
}
/* Code to Check the First element of Queue */
void front_ele()
{
if (ISEMPTY)
{
printf(EMPTY);
}
else
{
printf("The first element of the queue is: %d", queue[front]);
}
}
/* Code to Check the Size of Queue */
int queue_size()
{
int i = 0, count = 0;
if (ISEMPTY)
{
printf(EMPTY);
}
else
{
for (i = front;i <= rear;i++)
{
count++;
}
}
return count;
}
/* Code to destroy the queue */
void destroy()
{
int size, i;
if (ISEMPTY)
{
printf("EMPTY QUEUE CANNOT BE DESTROYED");
}
else
{
size = queue_size();
for (i = 0;i < size;i++)
{
front++;
}
front = 0;
rear = -1;
printf("\n\nQUEUE DESTROYED");
}
}
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[].
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
- Practice Design & Analysis of Algorithms MCQ
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Data Structure Books
- Check Programming Books