//The code will not run under most of the Compilers in Windows OS //Many of the declration techniques used the code doesnot work in old complilers like TURBO C //The code is tested in Code blocks 10.05 and GCC under LINUX operating system //download link for code blocks compliler http://www.codeblocks.org/downloads //It would be very easy to download and use code blocks if you are using Ubuntu linux distribution //Or else GCC can be used which comes along with any GNU/LINUX distributions #define n 5//Set the size of queue here #include<stdio.h> #include<stdbool.h> #include<stdlib.h> typedef struct { int key; }element; int rear=-1; int front=-1; element queue[n]; void queuefull(bool* fulltest)//This function is used to set the flag that the queue is full { *fulltest=false; } void queueempty(bool* emptytest)//This function is used to set the flag that the queue is empty { *emptytest=false; } void addq(int item,bool *fulltest,bool *emptytest)//This function is used to add elements to the Queue { *emptytest=true; rear=(rear+1)%(n); if(rear==front) queuefull(fulltest); else { queue[rear].key=item; if(rear==n-1&&front==-1)//if this line is not present than when no deletions are performed that is when front =-1, the loop runs again and again and overwrites the queue forth and back queuefull(fulltest); if((rear+1)%(n)==front) queuefull(fulltest); } } int deleteq(bool *emptytest,bool *fulltest)//This function is used to delete the queue elements { *fulltest=true; if(front==rear) queueempty(emptytest); else { front=(front+1)%(n); if(front==rear) queueempty(emptytest); return queue[front].key; } } void testfunction()//*This function is used to just print the values of front and rear { printf("**testing the vales front=%d,value=%d\trear=%d,value=%d*********",front,queue[front].key,rear,queue[rear].key); printf("\n"); } void showqueue(bool *emptytest,bool *fulltest)//This function is used to print the queue { int i; if(!*emptytest) { printf("\n*****Queue is empty******\n"); return ; } if((front==-1)||(front==-1&&!*fulltest)) { if(front==-1&&!fulltest) printf("\nQueue is full"); printf("\n\tHmmmm...You havent deleted any element yet and total no of elements=%d",rear+1); printf("\nThe elements are : "); for(i=0;i<=rear;i++) { printf("queue[%d]=%d",i,queue[i].key); printf(" "); } printf("\n"); return ; } if(!*fulltest) { printf("\nqueue is full and it contains (n-1) elements: "); if(rear>front) { for(i=front+1;i<=rear;i++) { printf("queue[%d]=%d \t",i,queue[i].key); } printf("\n"); } if(rear<front) { printf("\nstack is full and it contains (n-1) elements: "); for(i=front+1;i<n;i++) printf("queue[%d]=%d\t",i,queue[i].key); for(i=0;i<front;i++) printf("queue[%d]=%d\t",i,queue[i].key); printf("\n"); } return ; } if(1) { if(rear>front) { printf("\nThe total no.of.elements in the queue is: %d\n",rear-front); printf("\nHmmmm...There is still some space left in queue..its elements are : "); for(i=front+1;i<=rear;i++) { printf("queue[%d]=%d",i,queue[i].key); printf(" "); } printf("\n"); } if(rear<front) { printf("\nThe total no.of.elements in the queue is : %d\n ",((n-1)-front)+(front-rear)); for(i=front+1;i<n;i++) printf("queue[%d]=%d\t",i,queue[i].key); for(i=0;i<front;i++) printf("queue[%d]=%d\t",i,queue[i].key); } printf("\n"); } } int main() { int choice,item; bool fulltest=true;//the values true =1 and false =0 are defined in stdbool.h bool emptytest=false; for(;;) { testfunction(); if(fulltest&&emptytest) { printf("\n*****************Enter the operation you want to perform on the QUEUE************\n"); printf("1.ADD\n2.DELETE\n3.SHOW \n4.EXIT\n: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the item to be added to the queue: "); scanf("%d",&item); addq(item,&fulltest,&emptytest); break; case 2:deleteq(&emptytest,&fulltest); break; case 3:showqueue(&emptytest,&fulltest); break; default:exit(0); } } if(!fulltest) { testfunction(); printf("\n***QUEUE is full , you can perform only these following operations****"); printf("\n1.DELETE \n2.SHOW\n 3.EXIT\n: "); scanf("%d",&choice); switch(choice) { case 1:deleteq(&emptytest,&fulltest); break; case 2:showqueue(&emptytest,&fulltest); break; default:exit(0); } } if(!emptytest) { testfunction(); printf("\n******Queue is empty, only following operations are possible*********\n "); printf("1.ADD\n2.SHOW\n3.EXIT\n:"); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the item to be added to the queue: "); scanf("%d",&item); addq(item,&fulltest,&emptytest); break; case 2:showqueue(&emptytest,&fulltest); break; default : exit(0) ; } } } }
Advertisements