Sie sind auf Seite 1von 8

PRACTICAL-5

Name : Mohammed Salmanuddin

Class : SE-CMPN-1

ROLL NO : 60

BATCH : C-2

Title :- Implementation of Circular Queue Using Array

Theory:-
Circular Queue is a linear data structure , which follows the principle of the
FIFO(First In First Out) principle , but instead of ending the queue at the last
position, it starts again from the first position after the last, hence making the
queue behave like a circular data structure.

Circular Queue overcomes the drawback of the Linear Queue which is it saves us a
lot of memory, as in Linear Queue once an element is deleted , we cannot reused
the position again in the array.

Applications:-

1] Traffic Signal System

2] CPU scheduling

3] Memory management

Algorithm:-

1] For inserting an element in the circular queue:-


Step 1 :- Check if count == max ,

If yes , print out “Queue is Full”

Step 2 :- If statement == false

then else

ask the user for the element to be inserted == n

Step 3:- q.rear = (q.rear+1)%MAX

Step 4:- Assign the element to the q.rear

q.rear = n

Step 5:- count gets incremented by 1

Step 6 :- EXIT

2] For deleting an element in a circular queue :-

Step 1 :- Check if count == 0 ,

If yes , print out “Queue is Empty”

Step 2 :- If statement == false

then else

Step 3:- q.front = (q.front+1)%MAX

Step 4:- Delete the element in the circular queue q.x[q.front]

Step 5:- count gets decremented by 1

Step 6 :- EXIT

C Program :-
#include<stdio.h>

#include<stdlib.h>

#define MAX 5

struct cqueue

int front,rear;

int x[MAX];

}q;

void enqueue();

void dequeue();

void display();

int n, count=0;

int main()

int i,k;

printf("\n 1.Enqueue \n 2.Dequeue \n 3.Display \n 4.Exit");

q.front=-1; q.rear=-1; // Empty Circular Queue condition

while(1) {

printf("\n\t Enter your choice:");

scanf("%d",&k);

switch(k)

case 1:
enqueue();

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4: exit(0);

default: printf("\n\t Wrong Choice! Please enter a valid choice..");

return 0;

void enqueue() {

if(count==MAX)

printf("\n\t Queue is Full.");

else {

printf("\n\t Enter an element to be inserted:");

scanf("%d",&n);

q.rear=(q.rear+1)%MAX;

q.x[q.rear]=n;

count++;
}

void dequeue()

if(count==0)

printf("\n\t Queue is Empty");

else

q.front=(q.front+1)%MAX;

printf("\n\t Element deleted is=%d",q.x[q.front]);

count--;

void display()

int i;

if(count==0)

printf("\n\t Queue is Empty.");

else if(q.rear>q.front)

for(i=(q.front+1);i<=q.rear;i++)

printf("\n Queue contains:%d",q.x[i]);


}

else

for(i=(q.front+1);i<=(MAX-1);i++) {

printf("\n Queue contains:%d",q.x[i]);

for(i=0;i<=q.rear;i++)

printf("\n Queue contains:%d",q.x[i]);

OUTPUT:-

1] ENQUEUE OPERATION

2] DEQUEUE OPERATION

Das könnte Ihnen auch gefallen