deque - Coder's point

Saturday, April 6, 2019

deque

 #include<stdio.h>  
 #include<conio.h>              
 #define MAX 12  
 void addqatbeg(int *,int,int *,int *);  
 void addqatend(int *,int,int *,int *);  
 int delqatbeg(int *,int *,int *);  
 int delqatend(int *,int *,int *);  
 void display(int *);  
 int count(int *);  
 void main()  
 {  
 int arr[MAX];  
 int front,rear,i,n;  
 clrscr();  
 front=rear=-1;  
 for(i=0;i<MAX;i++)  
 arr[i]=0;  
 addqatend(arr,-12,&front,&rear);  
 addqatbeg(arr,88,&front,&rear);  
 addqatend(arr,23,&front,&rear);  
 addqatbeg(arr,21,&front,&rear);  
 addqatend(arr,167,&front,&rear);  
 addqatbeg(arr,18,&front,&rear);  
 addqatend(arr,12,&front,&rear);  
 addqatbeg(arr,19,&front,&rear);  
 addqatend(arr,15,&front,&rear);  
 addqatbeg(arr,-25,&front,&rear);  
 addqatend(arr,-6,&front,&rear);  
 addqatbeg(arr,56,&front,&rear);  
 printf("\nELEMENTS IN A DEQUE:");  
 display(arr);  
 n=count(arr);  
 printf("\nTOTAL NUMBER OF ELEMENTS IN DEQUE:%d",n);  
 i=delqatbeg(arr,&front,&rear);  
 printf("\nITEM EXTRACTED:%d",i);  
 i=delqatbeg(arr,&front,&rear);  
 printf("\nITEM EXTRACTED:%d",i);  
 i=delqatbeg(arr,&front,&rear);  
 printf("\nITEM EXTRACTED:%d",i);  
 printf("\nELEMENTS IN DEQUE AFTER DELETION:");  
 display(arr);  
 addqatend(arr,23,&front,&rear);  
 addqatbeg(arr,11,&front,&rear);  
 printf("\nELEMENTS IN DEQUE AFTER ADDITION:");  
 display(arr);  
 i=delqatend(arr,&front,&rear);  
 printf("\nITEM EXTRACTED:%d",i);  
 i=delqatend(arr,&front,&rear);  
 printf("\nITEM EXTRACTED:%d",i);  
 printf("\nELEMENTS IN DEQUE AFTER DELETION:");  
 display(arr);  
 n=count(arr);  
 printf("\nTOTAL NUMBER OF ELEMENTS IN DEQUE:%d",n);  
 getch();  
 }  
 void addqatbeg(int *arr,int item,int *pfront,int *prear)  
 {  
 int i,k,c;  
 if(*pfront==0 && *prear==MAX-1)  
 {  
 printf("\nDEQUE IS FULL.\n");  
 return;  
 }  
 if(*pfront==-1)  
 {  
 *pfront=*prear=0;  
 arr[*pfront]=item;  
 return;  
 }  
 if(*prear!=MAX-1)  
 {  
 c=count(arr);  
 k=*prear+1;  
 for(i=1;i<=c;i++)  
 {  
 arr[k]=arr[k-1];  
 k--;  
 }  
 arr[k]=item;  
 *pfront=k;  
 (*prear)++;  
 }  
 else  
 {  
 (*pfront)--;  
 arr[*pfront]=item;  
 }  
 }  
 void addqatend(int *arr,int item,int *pfront,int *prear)  
 {  
 int i,k;  
 if(*pfront==0 && *prear==MAX-1)  
 {  
 printf("\nDEQUE IS FULL.\n");  
 return;  
 }  
 if(*pfront==-1)  
 {  
 *prear=*pfront=0;  
 arr[*prear]=item;  
 return;  
 }  
 if(*prear==MAX-1)  
 {  
 k=*pfront-1;  
 for(i=*pfront-1;i<=*prear;i++)  
 {  
 k=i;  
 if(k==MAX-1)  
 arr[k]=0;  
 else  
 arr[k]=arr[i+1];  
 }  
 (*prear)--;  
 (*pfront)--;  
 }  
 (*prear)++;  
 arr[*prear]=item;  
 }  
 int delqatbeg(int *arr,int *pfront,int *prear)  
 {  
 int item;  
 if(*pfront==-1)  
 {  
 printf("\nDEQUE IS EMPTY.\n");  
 return 0;  
 }  
 item=arr[*pfront];  
 arr[*pfront]=0;  
 if(*pfront==*prear)  
 *pfront=*prear=-1;  
 else  
 (*pfront)++;  
 return item;  
 }  
 int delqatend(int *arr,int *pfront,int *prear)  
 {  
 int item;  
 if(*pfront==-1)  
 {  
 printf("\nDEQUE IS EMPTY.\n");  
 return 0;  
 }  
 item=arr[*prear];  
 arr[*prear]=0;  
 (*prear)--;  
 if(*prear==-1)  
 *pfront=-1;  
 return item;  
 }  
 void display(int *arr)  
 {  
 int i;  
 printf("\nFRONT->");  
 for(i=0;i<MAX;i++)  
 printf("\t%d",arr[i]);  
 printf("<-REAR");  
 }  
 int count(int *arr)  
 {  
 int c=0,i;  
 for(i=0;i<MAX;i++)  
 {  
 if(arr[i]!=0)  
 c++;  
 }  
 return c;  
 }  
OUTPUT:
ELEMENTS IN A DEQUE:                                                                                                            
FRONT-> 56      -25     19      18      21      88      -12     23      167                                                   1 
2       15      -6<-REAR                                                                                                        
TOTAL NUMBER OF ELEMENTS IN DEQUE:12                                                                                            
ITEM EXTRACTED:56                                                                                                               
ITEM EXTRACTED:-25                                                                                                              
ITEM EXTRACTED:19                                                                                                               
ELEMENTS IN DEQUE AFTER DELETION:                                                                                               
FRONT-> 0       0       0       18      21      88      -12     23      167                                                   1 
2       15      -6<-REAR                                                                                                        
ELEMENTS IN DEQUE AFTER ADDITION:                                                                                               
FRONT-> 0       11      18      21      88      -12     23      167     12                                                    1 
5       -6      23<-REAR                                                                                                        
ITEM EXTRACTED:23                                                                                                               
ITEM EXTRACTED:-6                                                                                                               
ELEMENTS IN DEQUE AFTER DELETION:                                                                                               
FRONT-> 0       11      18      21      88      -12     23      167     12                                                    1 
5       0       0<-REAR                                                                                                         
TOTAL NUMBER OF ELEMENTS IN DEQUE:9    

No comments:

Post a Comment