ARRAY IMPLEMENTATION OF QUEUE ADT

ALGORITHM: 

1. Define a array which stores queue elements.. 

2. The operations on the queue are 

a. a)INSERT data into the queue  

b. b)DELETE data out of queue 

3. INSERT DATA INTO queue 

a. Enter the data to be inserted into queue.  

b. If TOP is NULL 

i. The input data is the first node in queue.  

ii. The link of the node is NULL. 

iii. TOP points to that node.  

c. If TOP is NOT NULL 

i. The link of TOP points to the new node.  

ii. TOP points to that node. 

4. DELETE DATA FROM queue  

a. If TOP is NULL 

i. the queue is empty  

b. If TOP is NOT NULL 

i. The link of TOP is the current TOP. 

ii. The pervious TOP is popped from queue. 

5. The queue represented by linked list is traversed to display its content.

Program:

#include<stdio.h>  
#include<stdlib.h> 
#define SIZE 5 
int front = - 1; 
int rear = - 1; 
int q[SIZE]; 
void insert( ); 
void del( ); 
void display( ); 
void main( ) 
{ 
int choice; 
do 
{ 
printf("\t Menu"); 
printf("\n 1. Insert"); 
printf("\n 2. Delete"); 
printf("\n 3. Display "); 
printf("\n 4. Exit"); 
printf("\n Enter Your Choice:"); 
scanf("%d", &choice); 
switch(choice) 
{ 
case 1: 
insert( ); display( ); break; 
case 2: 
del( ); display( ); break; 
case 3:display( );
break; 
case 4: 
printf("End of Program....!!!!"); 
exit(0); 
}}while(choice != 4);} 
void insert( ) 
{ 
int no; 
printf("\n Enter No.:"); 
scanf("%d", &no); 
if(rear < SIZE - 1) 
{ 
q[++rear]=no; 
if(front == -1) 
front=0;// front=front+1; 
} 
else 
{ 
printf("\n Queue overflow"); 
}} 
void del( ) 
{ 
if(front == - 1) 
{ 
printf("\n Queue Underflow"); 
return; 
} 
else
{ 
printf("\n Deleted Item:-->%d\n", q[front]); 
} 
if(front == rear) 
{ 
front = - 1; 
rear = - 1; 
} 
else 
{front = front + 1; 
}} 
void display( ) 
{ 
int i; 
if( front == - 1) 
{ 
printf("\nQueue is empty...."); 
return; 
} 
for(i = front; i<=rear; i++) 
printf("\t%d",q[i]);} 


Output:

Menu
 1. Insert
 2. Delete
 3. Display 
 4. Exit
 Enter Your Choice:1

 Enter No.:123456
	123456Menu
 1. Insert
 2. Delete
 3. Display 
 4. Exit
 Enter Your Choice:4
End of Program....!!!!

Leave a comment