In today's session, we are going to learn about a program in c to implement Circular Queue. Circular Queue implementation using array is one of the simplest techniques to understand its functionality. for implementing Queue using array first there is a need to know how circular Queue works. Circular queue work on the principle of FIFO (first in first out). The program consists of two-part
Write a program to implement the Circular Queue using Array.
- First, we provide a choice of operation that can be performed on the Circular Queue like add, delete using Switch case.
- Second, we provide an implementation of the function used in Switch Case
Write a program to implement the Circular Queue using Array.
/*
File: DSPrgm10.c
Author: Aditya Saini
Date: Nov 12, 2019
Description: Program to implement the Circular Queue using Array.
*/
#include <stdio.h>
#define SIZE 5
//Declare an array for Queue
int queue[SIZE];
int front = -1;
int end = -1;
int count = 0;
void enqueue (void);
void dequeue (void);
void display (void);
//Main function
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Display\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
enqueue ( );
break;
case 2:
dequeue ( );
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice.\n");
}
}
};
//Function to enqueue
void enqueue (void)
{
int element;
if (count >= SIZE)
{
printf ("Error! Queue Full\n\n");
return;
}
printf ("Input element: ");
scanf ("%d", &element);
if (count == 0)
{
front = 0;
end = -1;
}
else if (end == SIZE - 1)
{
end = -1;
}
queue[++end] = element;
count++;
};
//Function to dequeue
void dequeue (void)
{
if (count <= 0)
{
printf ("Error! Empty Queue\n\n");
return;
}
printf ("%d deleted\n\n", queue[front]);
if (count == 1)
{
front = -1;
end = -1;
}
else if (front == SIZE - 1)
{
front = 0;
}
else
{
front++;
}
count--;
};
//Function to display the elements in the queue
void display (void)
{
int i = front;
if (i > end)
{
while (i <= SIZE - 1)
{
printf(" %d ", queue[i++]);
}
i = 0;
}
while (i <= end)
{
printf(" %d ", queue[i++]);
}
};
File: DSPrgm10.c
Author: Aditya Saini
Date: Nov 12, 2019
Description: Program to implement the Circular Queue using Array.
*/
#include <stdio.h>
#define SIZE 5
//Declare an array for Queue
int queue[SIZE];
int front = -1;
int end = -1;
int count = 0;
void enqueue (void);
void dequeue (void);
void display (void);
//Main function
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Display\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
enqueue ( );
break;
case 2:
dequeue ( );
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice.\n");
}
}
};
//Function to enqueue
void enqueue (void)
{
int element;
if (count >= SIZE)
{
printf ("Error! Queue Full\n\n");
return;
}
printf ("Input element: ");
scanf ("%d", &element);
if (count == 0)
{
front = 0;
end = -1;
}
else if (end == SIZE - 1)
{
end = -1;
}
queue[++end] = element;
count++;
};
//Function to dequeue
void dequeue (void)
{
if (count <= 0)
{
printf ("Error! Empty Queue\n\n");
return;
}
printf ("%d deleted\n\n", queue[front]);
if (count == 1)
{
front = -1;
end = -1;
}
else if (front == SIZE - 1)
{
front = 0;
}
else
{
front++;
}
count--;
};
//Function to display the elements in the queue
void display (void)
{
int i = front;
if (i > end)
{
while (i <= SIZE - 1)
{
printf(" %d ", queue[i++]);
}
i = 0;
}
while (i <= end)
{
printf(" %d ", queue[i++]);
}
};
Output
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 9
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 2
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue: 9 2 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
9 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
2 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Input element to insert: 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 9
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 2
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue: 9 2 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
9 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
2 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Input element to insert: 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4
No comments:
Post a Comment
Please do not post spam links.