| #include<iostream.h> #include<conio.h> typedef int itemType; struct node{ itemType item; node* next; }; typedef struct node* ptrType; class Queue{ private: ptrType rear; public : Queue(); Queue(const Queue&q); ptrType getFrontPtr(); int add(itemType); int delet(); int getFront(itemType &); int isEmpty(); int isFull(ptrType); void show(); }; //----------------------------------------------------- Queue::Queue(){rear=NULL;} //--------------------------------------------------- Queue::Queue(const Queue &q) { ptrType temp=q.getFrontPtr(); do{ add(temp->item); temp=temp->next; } while(temp!=q.getFrontPtr()); } //------------------------------------------------ ptrType Queue::getFrontPtr(){ return rear->next;} //------------------------------------------------ int Queue::isEmpty(){return rear==NULL;} //------------------------------------------------ int Queue::isFull(ptrType temp){return (temp==NULL);} //---------------------------------------------- int Queue:: add(itemType newItem){ ptrType temp= new node; if(isFull(temp)) { cout<<"\n Full Queue"; return 0; } else { if(isEmpty()) { temp->next=temp; } else { temp->next=rear->next; rear->next=temp; } temp->item=newItem; rear=temp; return 1; } } //------------------------------------------------- int Queue::delet(){ if(isEmpty()) { cout<<"\n Empty Queue"; return 0; } else { ptrType front =rear->next; if(front==rear) //one node rear=NULL; else rear->next=front->next; delete front; return 1; } } //-------------------------------------------- int Queue::getFront(itemType &item) { if(isEmpty()) { cout<<"\n Empty Queue"; return 0; } else { item=rear->next->item; return 1; } } //------------------------------------------- void Queue::show(){ if(isEmpty()) { cout<<"\n Empty Queue"; return ; } else { ptrType temp=rear->next; do{ cout<<temp->item<<"-"; temp=temp->next; }while(temp!=rear->next); } } //--------------------------------------------- void main(){ class Queue store,cashier; int time=0,start,duration,entry,shop,service, shopCount=0,cashierCount=0,max=0,x; cout<<"\n All timings entered should be in minutes \n "; cout<<"\n Enter the simulation time : "; cin>>duration; cout<<"\n Enter the customer entry intervals : "; cin>>entry; cout<<"\n Enter the customers shoppig time : "; cin>>shop; cout<<"\n The cashier service time :"; cin>>service; do{ ++time; cout<<"\n The time in minutes"<<time; // (1) time has come to accept new customer if(time%entry==0) { store.add(time); ++shopCount; cout<<"\n A customer started shopping- current shoppers : "; store.show(); getch(); } // (2) customer at the front of the shop queue has finished shopping if(!store.isEmpty()) { store.getFront(start); if(time==(start+shop)) { store.delet(); cashier.add(start); ++cashierCount; cout<<"\n A customer finished shopping , current shoppers : "; store.show(); cout<<"\n A customer joined the cashier queue"; cashier.show(); getch(); if( max < cashierCount) max = cashierCount; } } // (3) customer at the front of the cashierqueue has finished shopping if(!cashier.isEmpty()) { cashier.getFront(start); if(time==(start+shop+service)) { cashier.delet(); --cashierCount; cout<<"\n A customer exited- the cashier queue changed : "; cashier.show(); getch(); } } }while(time<duration); cout<<"\n Enter the simulation time : "<< duration; cout<<"\n Enter the customer entry intervals : "<<entry; cout<<"\n Enter the customers shoppig time : "<<shop; cout<<"\n The cashier service time :"<<service; cout<<"\n Total shoppers in the given time period were"<<shopCount; cout<<"\n The highest length of the cashier queue"<<max; }//end main |