Queue using array | Queue in data structure | Operation on queue

In this article Queue using array we give the information about queue in data structure, operation on queue, queue definition, define queue and add and remove queue operations in data structure

Queue using array:

Introduction to Queue:

A line of people, vehicles or other objects, in which the person or object at the found end is deal with first, the one behind is deal with next, and so on, and which newcomers join at the opposite end is the working of queue.

Queue is a waiting list or other means of organizing people or object into a First-In-First-Out (FIFO) order.

In computer queue is a first-in-first-out data structure used to sequence multiple demands for a resource such as a printer, processor or communications channel.

Definition of queue:

Queue is an ordered list of elements in which the element is added only at one end called rear of the queue and deleted from only one end called front of the queue.

Queue is a non-primitive, linear data structure.

Operation on Queue:

The basic operations that can perform on linear queue are:

  1. To insert an element in a queue: Add or Insert
  2. To delete an element from the queue: Delete or Remove

Additional operations can be defined:

IsEmpty():     Reports whether the queue is empty or not.

IsFull():          Reports whether the queue is full or not.

Following figures explain the operation on queue.

1. Add Or Insert:

The process of adding elements into the queue from the rear called as add or insertion operation.

Queue is empty

Insert 15

15

Front       Rear

Insert 25

15

25

Front

Rear

Insert 35

15

25

35

Front

 

Rear

Insert 45

15

25 35

45

Front

   

Rear

Insert 55

15

25 35 45

55

Front

     

Rear

2. Delete or Remove:

The process of deleting or removing the element from the front is called as delete or removes option.

Queue is full

15

25 35 45 55
Front      

Rear

Delete 15

 

25

35 45

55

  Front    

Rear

Delete 25

 

 

35 45

55

 

 

Front  

Rear

Delete 35

     

45

55

   

 

Front

Rear

Delete 45

       

55

       

Front      Rear

Delete 55, now queue is empty

         


Declaration of Queue:

A queue is implemented either statically or dynamically. Array is used to implement queue statically and linked list is used to implement queue dynamically. In this article, we shall implement queue statically.

Queue is an ordered list of elements in which the element is added only at one end called rear of the queue and deleted from only one end, called front of the queue.

A queue to be implemented using array requires:-

  1. A fixed size array to hold elements in queue.
  2. Two integers to indicate rear and front position of an element.

Rear keeps the status of last added item in queue and front keeps the status of first item of queue.

Using structure queue is declared as follows:

Syntax:

#define queue

{

int data[ MAX ];

int front, rear;

};

This represents only template of queue. Actually, queue is declared as-

struct queue q;

1.Add operation in queue:

For adding the element in queue first check, the conditions of overflow then add the element.

int IsFull()

{

if( q.rear = = MAXSIZE – 1)

return 1;

else

return 0;

}

void insert ( int no )

{

if( q.front = = -1)

q.front=0;

q.rear = q.rear + 1;

q.data [ q.rear ] = no;

printf(“\n %d is inserted into QUEUE….”,no);

}

2.Delete operation in queue:

For deleting the element in queue, first check then condition of underflow and then delete the element.

int IsEmpty( )

{

if( q.front = = -1 | | q.front > q.rear )

return 1;

else

return 0;

}

void delete( )

{

int temp;

temp= q.data [ q.front ];

q.front = q.front + 1;

printf(“\n %d element is deleted…”, temp);

}

3.Display elements of queue:

void display()

{

int i;

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

printf(“%d”, q.data[i]);

}

// This program implements the insert and delete operations in linear queue

#define MAX 4

void insert(int);

void delete();

void display();

struct queue

{

int front,rear;

int data[MAX];

}

struct queue q;

void main()

{

int n, ch;

q.front=q,rear=-1;

while(1)

{

printf(“\n MENU”);

printf(“\n1.Insert\n2.Delete\n3.Display\n4.Exit”);

printf(“\n Enter your Choice : “);

scanf(“%d”,&ch);

switch(ch)

{

case 1:

if(isfull())

printf(“\n Queue Overflow….”);

else

{

printf(“\n Enter number to insert: “);

scanf(“%d”,&n);

insert(n);

}

break;

case 2:

if(isempty())

printf(“\n Queue Underflow…..”);

else

delete();

break;

case 3:

if(isempty())

printf(“\n Queue Underflow……”);

else

display();

break;

case 4:

exit(0);

default: printf(“\n Enter correct Choice….1 to 4 only”);

break;

}

}

}

int isfull()

{

if(q.rear==MAX-1)

return (1);

else

return (0);

}

int isempty()

{

if(q.front==-1 || q.front>q.rear)

return (1);

else

return (0);

}

void insert(int no)

{

if(q.front==-1)

q.front=0;

q.rear=q.rear+1;

q.data[q.rear]=no;

printf(“\n %d is inserted into Queue…..”, no);

}

void delete()

{

int temp;

temp=q.data[q.front];

q.front=q.front+1;

printf(“\n %d element is deleted …..”,temp);

}

void display()

{

int i;

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

printf(“%d”,q.data[i]);

}

Applications of Queue:-

1:- Queue is used in CPU scheduling and disk scheduling. When multiple processes require CPU at the same time, different CPU scheduling algorithms are used in this by implementing queue.

2:- It is used to transfer data between two processes in asynchronous manner. In this queue is used for synchronization. For example- IO buffers, pipes, file IO etc.

3:- It is used in print spooling.

4:- In graph, it is in BFS (breadth first search). BFS is an algorithm in the data structure that traverses and searches the graph.

5:- In handling the interrupts occurring in the real time system. As soon as interrupts are generated they are handled in the same order. The interrupt that will be generated first is handled first.

6:- In real life, the phone systems of the call center also use the queue. It is used to hold the customers’ calls in an order. Unless an executive becomes free.

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *