Queue using array program blueprint

Declare an array to implement queue
int queue[MAX]                (MAX is the size of the array / queue)

Declare and initialize front and rear to -1
int front = -1;
int rear = -1;Create functions

void insert()                (to insert elements into the queue)
int pop()                     ( to delete elements from the queue)
void display()             (to display elements of the queue)
Insert elements into the queue
In the insert function first check if the queue is full because in case the queue is full then we will not be able to insert any elements into the queue.

void insert(int element)
{
           
if(rear==MAX-1)
{
       printf("nQueue is Fulln");
       return;            
}
           
if(front==-1) 
{
       front = 0; 
}
rear=rear+1;
queue[rear]=element;
}

If the queue is full then queue is full will be printed and with the help of that return statement we will come of of the insert function or in other words the insert function will terminate.

If the queue is not full then the following statements will be executed.
if(front==-1)
 {
 front = 0; 

rear=rear+1; 
queue[rear]=element;
 

This if statement, if(front==-1) will be true only during the first insertion.
First time when an element is inserted into the queue then the value of front from -1 will become equal to zero with the help of this if statement in
the insert function.
 
if(front==-1) 

front = 0;
 }rear=rear+1;  Value of rear will be incremented by 1 and a new element will be inserted
into the queue with the help of this statement
queue[rear]=element; 

int del()
{
    int element;
    if(front==-1 || front==rear+1)
    {
                 printf("Queue is Emptyn");
                 return;
    }
    element = queue[front];
    front = front + 1;
    printf("%d has been deletedn", element);
    return element;
}

If the queue is full then queue is full will be printed and that return statement will terminate the del function.

If the queue is not full the the following statements will be executed.
element = queue[front];
front = front + 1;
printf(“%d has been deletedn”, element);
return element;

Variable element will store the value that will be deleted from the queue.

An Element in the queue is deleted by incrementing the value of front by 1 which is done using this statement.
front = front + 1;


printf(“%d has been deletedn”, element);    This print statement prints the value that has been deleted from the queue.

Previous Post
Queue using array
Next Post
C program to implement queue using array

Related Posts

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu