Exercise 3
b) Consider a payment counter at which the customer pays for the items purchased. Every time a customer finished paying for their items, he/she leaves the queue from the front. Every time another customer enters the line to wait, they join the end of the line. Implement the application for this problem.
Program::
#include< stdio.h>//Standard i/p,o/p header file void fill();//prototype of function enque void delete();void display();int queue[20],remv[20],temp[20];//Declaration of array int f=-1,r=-1;//f-front,r-rear intialisation int int h,i,n,t,j;//h-for switch case,n-for size of queue int main()//main function { printf("Enter the size of queue : "); scanf("%d",&n); while(h!=4) { printf(" 1-for insert\n 2-for delete \n 3-for display \n 4-for exit \n Enter ur choice: "); scanf("%d",&h); switch(h) { case 1:fill(); //calling functionbreak; case 2:delete();break; case 3:display();break; case 4:break; default:printf("Invalid entry \n"); } } }void fill() { j=0;if (r==n-1) { printf("Queue is full \n"); display(); printf("wait for sometime to send another person \n"); }else { r++;if (f< 0||queue[r-1]< n) queue[r]=queue[r-1]+1;else if (queue[r-1]>=n ||queue[r-2]>=n) { queue[r]=remv[j]; j++; f--; } printf("Filled position is %d \n",queue[r]); } }void delete() {if (r==-1) printf("Queue is empty deletion not possible\n");else { f++; remv[f]=queue[0]; printf("The removed position is %d \n",remv[f]); // printf("%d \t %d",f,remv[f]); t=r; }for (i=0;i<=t;i++) { queue[i]=queue[i+1]; } r--; }void display() { printf("\n The filled positions in the queue are \n");if (r==-1) printf(" Null \n");else { t=r;for (i=0;i<=t;i++) printf("%d \t",queue[i]); printf("\n"); } printf("\n Th1e vacant positions in the queue are \n");if (r==n-1) printf(" Null \n");else { temp[0]=queue[r]; i=-1;for (j=1;j< n-r;j++) { temp[j]=temp[j-1]+1;if (temp[j]>n) { i++; printf("%d \t",remv[i]); }if (temp[j-1]<=n-1) { printf("%d\t ",temp[j]=temp[j-1]+1); } } } printf("\n"); }
output::
Enter the size of queue : 7 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 1 Filled position is 1 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 1 Filled position is 2 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 1 Filled position is 3 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 1 Filled position is 4 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 3 The filled positions in the queue are 1 2 3 4 Th1e vacant positions in the queue are 5 6 7 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 2 The removed position is 1 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 2 The removed position is 2 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 2 The removed position is 3 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 2 The removed position is 4 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice: 3 The filled positions in the queue are Null Th1e vacant positions in the queue are 1 2 3 4 5 6 7 1-for insert 2-for delete 3-for display 4-for exit Enter ur choice:4
No comments:
Post a Comment