Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

Queue->Array implementation->Insert operation

It's going to be a static implementation of queues and good for limited elements to be handled, as you know array size has to be pre-defined and can't be changed dynamically. Here, every time you insert a new element in the queue you'll check for the queue 'overflow' condition as we are using a static array to implement the queue.   

Here, I'll take rear as -1 and front as 0 when the Queue is empty.

Algorithm:-
Step-1: Increment the 'rear' by 1
Step-2: Check for the 'queue overflow' condition. If queue not overflowing then go to step-3 else say "queue overflow" and quit
Step-3: Put the new element at the position pointed by 'rear'

C implementation:-
static int queue[max];
int front = 0, rear = -1;
..
..
insert(int new)
{
 rear = rear + 1;
 if(rear < max)
   queue[rear] = new;
 else
   printf("Queue overflow");
}

Related Operations:
Queues - Concepts
Array implementation - add( ) and delete( )
Linked List implementation - add( ) and delete( )
Circular queues - add( ) and delete( )

Index || Doubts / Clarifications || Related Topics || Web Links