Basic Logic
Queue Structure: The queue is a data structure where elements are added at the back and removed from the front.
Pointers:
headpoints to the first element in the queue (front).tailpoints to the next available position for insertion (back).- The
headis incremented after each removal, andtailis incremented after each insertion.
Variables
int n: Size of the queue.int head = -1, tail = -1: Initialize theheadandtailpointers to indicate an empty queue.stack[n]: Array of integers or characters used as the queue.ele: The value to be added to the queue (enqueue operation).ch: Integer variable used to store the user’s choice for queue operations.
Implementation Logic
Global Variables:
- Declare
n,head = -1, andtail = -1globally to manage the queue. - Functions for queue operations (
enque,deque, anddisplay) are declared, all of which returnvoid.
Input Handling:
- In the
main()function, the size of the queue is taken as input (scanf("%d", &n)). - Declare
stack[n]to store the queue elements andelefor input values of relevant type.
- A
while(1)loop used to continuously display options and perform chosen operation. - Declare
int chto hold the choice of user. - make
if elseconditions for each function, an exit and miss typed number. - Only option for
enqueneeds to be passed the value to be added withstack
Function Logic
enque:
- Checks if the queue is full (
tail == n-1). - If not full, it increments
tailand inserts the element intostack[tail]. - If
headis-1(first insertion), it incrementsheadto indicate that the queue is no longer empty.dequ: - Removal happens at the head, so check if (
head == -1 || head > tail) which means queue is empty - If not empty, it removes the element at
headand incrementshead.display: - Checks if the queue is empty (
head == -1 || head > tail). - If not empty, it displays all elements from
headtotail.
- run a for loop with
i = frontand running tilli <= tailwithi++ - Print each
stack[i]
Implementation
c
#include <stdio.h>
#include <stdlib.h>
int n, head = -1, tail = -1;
void enqu(int stack[], int x);
void dequ(int stack[]);
void display(int stack[]);
void main()
{
printf("Enter the size of the Queue \n");
scanf("%d", &n);
int stack[n], ele;
while(1)
{
int ch;
printf("Make a choise:\n");
printf("\n0.Exit\t1.Enqueue\n2.Dequeue\t3.Display\n");
scanf("%d", &ch);
if (ch == 0)
exit(0);
else if (ch == 1)
{
printf("\nProvide the Number to Add: \n");
scanf("%d", &ele);
enqu(stack, ele);
}
else if (ch == 2)
dequ(stack);
else if (ch == 3)
display(stack);
else
printf("Enter a Proper choice\n");
}
}
void enqu(int stack[], int x)
{
if ( tail == n-1)
{
printf("\nThe Queue is Full\n");
}
else
{
if ( head == -1)
head++;
tail++;
stack[tail] = x;
printf("\nNumber %d has been inserted in the Queue\n\n", x);
}
}
void dequ(int stack[])
{
if ( head == -1 || head > tail)
printf("\nThe Queue is empty\n");
else
{
printf("The Number %d has been removed\n", stack[head]);
head++;
}
}
void display(int stack[])
{
if ( head == -1 || head > tail)
printf("\nThe Queue is empty\n");
else
{
printf("The Elements in the Queue are: \n");
for( int i = head; i <= tail; i++)
{
printf("%d \t", stack[i]);
}
printf("\n");
}
}