Queue

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Linear Queue

ALGORITHM FOR LINEAR QUEUE:


ENQUEUE :
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT.
DEQUEUE:
Step 1 - Check whether queue is EMPTY. (front == rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the
function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i =
front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the
same until 'i' value reaches to rear (i <= rear).
Traverse
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT.

CODE:

#include <stdio.h>

#define MAX 50

void insert();
void delet();
void display();
int queue_array[MAX];
int r = - 1;
int f = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice \n");
}
}
}

void insert()
{
int add_item;
if (r == MAX - 1)
printf("Queue Overflow \n");
else
{
if (f == - 1)

f = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
r = r + 1;
queue_array[r] = add_item;
}
}

void delet()
{
if (f == - 1 || f > r)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[f]);
f = f + 1;
}
}

void display()
{
int i;
if (f == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = f; i <= r; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Circular Queue

ALGORITHM:
Algorithm to enqueue an element in a circular queue:

Step 1: IF (REAR+1)%MAX = FRONT


Write " OVERFLOW "
Goto step 4
[End OF IF]

Step 2: IF FRONT = -1 and REAR = -1


SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]

Step 3: SET QUEUE[REAR] = VAL.


Algorithm to dequeue an element from the circular queue

Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT].

TRAVERSE:
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]
Step 4: EXIT\.

CODE:
#include <stdio.h>

# define max 6
int queue[max];
int f=-1;
int r=-1;

void enqueue(int element)


{
if(f==-1 && r==-1)
{
f=0;
r=0;
queue[r]=element;
}
else if((r+1)%max==f)
{
printf("Queue is overflow..");
}
else
{
r=(r+1)%max;
queue[r]=element;
}
}

int dequeue()
{
if((f==-1) && (r==-1))
{
printf("\nQueue is underflow..");
}
else if(f==r)
{
printf("\nThe dequeued element is %d", queue[f]);
f=-1;
r=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[f]);
f=(f+1)%max;
}
}

void display()
{
int i=f;
if(f==-1 && r==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=r)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;

while(choice<4 && choice!=0)


{
printf("\nPress 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\npress 4: exit");
printf("\nEnter your choice : ");
scanf("%d", &choice);

switch(choice)
{

case 1: printf("Enter the element which is to be inserted :


");
scanf("%d", &x);
enqueue(x);
break;

case 2: dequeue();
break;

case 3: display();

case 4: exit(1);
default:
printf("Wrong choice \n");

}
return 0;
}

OUTPUT:

You might also like