Queue - Coder's point

Sunday, April 7, 2019

Queue

 #include<stdio.h>  
 #include<conio.h>  
 #define MAX 11  
 void addq(int*,int,int*,int*);  
 int delq(int*,int*,int*);  
 void main()  
 {  
 int arr[MAX],i;  
 int front=-1,rear=-1;  
 clrscr();  
 addq(arr,2,&front,&rear);  
 addq(arr,9,&front,&rear);  
 addq(arr,11,&front,&rear);  
 addq(arr,-10,&front,&rear);  
 addq(arr,25,&front,&rear);  
 addq(arr,16,&front,&rear);  
 addq(arr,17,&front,&rear);  
 addq(arr,22,&front,&rear);  
 addq(arr,19,&front,&rear);  
 addq(arr,30,&front,&rear);  
 addq(arr,32,&front,&rear);  
 i=delq(arr,&front,&rear);  
 printf("\nitem deleted: %d",i);  
 i=delq(arr,&front,&rear);  
 printf("\nitem deleted: %d",i);  
 i=delq(arr,&front,&rear);  
 printf("\nitem deleted: %d",i);  
 getch();  
 }  
 void addq(int *arr,int item,int *pfront,int *prear)  
 {  
 if(*prear==MAX-1)  
 {  
 printf("\nqueue is full");  
 }  
 (*prear)++;  
 arr[*prear]=item;  
 if(*pfront==-1)  
 *pfront=0;  
 }  
 int delq(int *arr,int *pfront,int *prear)  
 {  
 int data;  
 if(*pfront==-1)  
 {  
 printf("\nqueue is empty");  
 return NULL;  
 }  
 data=arr[*pfront];  
 arr[*pfront]=0;  
 if(*pfront==*prear)  
 *pfront=*prear=-1;  
 else  
 (*pfront)++;  
 return data;  
 }  
OUTPUT:
item deleted :2
item deleted :9
item deleted :11 

No comments:

Post a Comment