Need help making a bank queue and list. I Have program almost complete.
#include <iostream> #include <cstdlib> #include "List.h" #include "Queue.h" using namespace std; void tList(); void tQueue(); void processArrival(int arrivTime, int transTime, int customer, List &eventList, Queue &bankQueue); void processDeparture(int departTime, List &eventList, Queue &bankQueue); struct dataRecord{ int arrivTime; int transTime; }; dataRecord inputFile[4]={{20,5},{22,4},{23,2},{30,3}}; int waitingTime=0; int main(){ Queue bankQueue; List eventList; eventList.addNode('A', inputFile[0].arrivTime); int customer=0; int arrivTime=0; int transTime=0; int departTime=0; while(!eventList.isEmpty()){ eventList.getEventStatus(); if(eventList.getEventStatus()=='A'){ processArrival(inputFile[customer].arrivTime, transTime, inputFile[customer].transTime, eventList, bankQueue); customer++; } else{ processDeparture(departTime, eventList, bankQueue); } cout<<"Arrival Time: "<<arrivTime<<endl; cout<<"Departure Time: "<<departTime<<endl; cout<<"Transaction Time: "<<transTime<<endl; } double avgTime=(waitingTime*1.0)/customer; cout<<"Average wait: "<<avgTime<<endl; return 0; } void processArrival(int arrivTime, int transTime, int customer, List &eventList, Queue &bankQueue){ int occurTime=0; if(bankQueue.isEmpty()){ inputFile->transTime; eventList.getEventStatus()=='D'; occurTime=arrivTime+transTime; } bankQueue.equeue(arrivTime, transTime); if(inputFile!=NULL){ eventList.getEventStatus()=='A'; eventList.sort(); } } void processDeparture(int departTime, List &eventList, Queue &bankQueue){ int occurTime=0; int transTime=0; bankQueue.dequeue(); eventList.removeNode(1); if(!bankQueue.isEmpty()){ eventList.getEventStatus()=='D'; occurTime=departTime+transTime; eventList.sort(); waitingTime=departTime-bankQueue.getFrontArrivTime(); } } /*void tList(){ List eventList; eventList.addNode('A',20); eventList.addNode('D',22); eventList.addNode('A',23); eventList.addNode('D',25); eventList.showContent(); eventList.sort(); eventList.removeNode(0); eventList.showContent(); eventList.addNode('A', 29); eventList.sort(); eventList.showContent(); } void tQueue(){ Queue bankQueue; bankQueue.equeue(20,5); bankQueue.equeue(22,4); bankQueue.equeue(23,2); bankQueue.equeue(25,3); bankQueue.showContent(); bankQueue.dequeue(); bankQueue.equeue(29,5); bankQueue.showContent(); }*/
Thats the main file.
Headers are here: #include<cstdlib> #include<iostream> using namespace std; class List{ public: List(){ size=0; head=NULL; tail=NULL; } void setSize(int s){ size=s; } int getSize(){ return size; } void showContent(){ Node *cur=head; cout<<"List"<<endl; while(cur!=NULL){ cout<<cur->eventStatus<<" "<<cur->occurTime<<endl; cur=cur->next; } return; } void addNode(char status, int time){ Node *newNode= new Node; newNode->eventStatus=status; newNode->occurTime=time; newNode->next=head; head=newNode; size++; } void removeNode(int index){ if(index<0||index>=size) return; else if(index==0) return removeFirst(); else if(index==size-1) return; else{ Node *Previous=head; for(int i=1;i<index;i++){ Previous=Previous->next; } Node *cur=Previous->next; Previous->next=cur->next; size--; int occurTime=cur->occurTime; char eventStatus=cur->eventStatus; delete cur; return; } } void removeFirst(){ if(size==0) return; else{ Node *temp=head; head=head->next; size--; if(head==NULL) tail=NULL; char eventStatus=temp->eventStatus; int occurTime=temp->occurTime; delete temp; return; } } void removeLast(){ if(size==0) return; else if(size==1){ Node *temp=head; size=0; char eventStatus=temp->eventStatus; int occurTime=temp->occurTime; delete temp; return; } else{ Node *cur=head; for(int i=0;i<size-2;i++) cur=cur->next; Node *temp=tail; tail=cur; tail->next=NULL; size--; char eventStatus=temp->eventStatus; int occurTime=temp->occurTime; delete temp; return; } } void sort(){ Node *cur=head; char a; int b; if (size<=1){ return; } for (int i=0;i<size;i++){ cur=head; while (cur->next!=NULL){ if (cur->occurTime>cur->next->occurTime){ a=cur->eventStatus; cur->eventStatus=cur->next->eventStatus; cur->next->eventStatus=a; b=cur->occurTime; cur->occurTime=cur->next->occurTime; cur->next->occurTime=b; } cur=cur->next; } } return; }//sorted by OccurTime char getEventStatus(){ return head->eventStatus; } int getEventTime(){ return head->occurTime; }//return the value of OccurTime bool isEmpty(){ return (head=NULL); } private: struct Node { char eventStatus; int occurTime ; Node *next; }; int size; Node *head; Node *tail; Node *findNode(int index){ if (index<1||index>getSize()) return NULL; else{ Node *cur=head; for(int i=0;i<index;i++) cur=cur->next; return cur; } } };
#include<cstdlib> #include<iostream> #include<iomanip> using namespace std; class Queue{ public: Queue(){ frontPtr=NULL; backPtr=NULL; size=0; } void setSize(int s){ size=s; } void showContent(){ Node *cur=frontPtr; if(isEmpty()){ cout<<"Nothing in the queue"<< endl; return; } cout<<"Aririval Time"<<setw(19)<<"Transaction Time"<<endl; while(cur!=NULL){ cout<<cur->arrivTime<<setw(15)<<cur->transTime<<endl; cur=cur->next; } cout << endl; } void equeue(int arrivTime, int transTime){ Node *newNode=new Node; newNode->arrivTime=arrivTime; newNode->transTime=transTime; newNode->next=NULL; if (backPtr==NULL){ frontPtr=newNode; backPtr=newNode; size++; return; } else{ backPtr->next=newNode; backPtr=backPtr->next; size++; return; } } void dequeue(){ if(size==0){ return; } else{ Node *temp=frontPtr; frontPtr=frontPtr->next; temp->next=NULL; delete temp; size--; } } int getFrontTransTime(){ return frontPtr->transTime; } int getFrontArrivTime (){ return frontPtr->arrivTime; } bool isEmpty(){ return (size==0); } private: struct Node { int arrivTime; int transTime; Node *next; }; Node *frontPtr; Node *backPtr; int size; };
This is the Assignment. Algorithm: 1. Using an array to store the data information: time of each customer arrives and the time it requires for each customer to compete the transaction. Using following data type for each element of the array: struct dataRecord { int arrivTime; int transTime; }; Above array can be declared outside of main() function (global array) to simplify the program. 2. Create an empty queue called bankQueue, to represent the bank line, and create an empty list call eventList for recording the arrival and departure events. 3. Read the data of the first customer from the information array and place them in the even list by letting eventStatus=’A’ and occurTime= the arrival time of the first customer 4. Declare an integer variable customer and initialize it to 0 5. Start a looping statement on the event list: while(event list is not empty){ Retrieve the status of the event in the front of the event list: if(The event is an arrival event){ Process arrival event; customer++; //advance to the next customer } else Process departure event; }//end of the while loop 6. While the program is processing the arrival event or departure event, calculate and display the time table that shows the time when a customer arrives, departs, and begins a transaction 7. After the while loop, display the average waiting time. (total waiting time is calculated during the while loop. ---------------------------------------------------------------------------------------------------------------------------- To process an arrival event, using following function: void processArrival( int arrivTime, int transTime, int customer, List &eventList, Queue &bankQueue) { 1. Check if the bankQueue is empty; 2. If (the bankQueue is empty){ Insert a departure event in the eventList by letting eventStatus = ‘D’ and occurTime = arrivTime + transTime } 3. Insert a customer into the bankQueue 4. Delete the arrival event from the eventList 5. If(not at the end of the information array){ a. Read the next customer information from the array and add an arrival event to the eventList by letting eventStatus = ‘A’ and occurTime = arrival time of the customer b. Sort the eventList in ascending order. } } ----------------------------------------------------------------------------------------------------------------------------------------------------- To process a departure event, using following function: void processDeparture(int departTime, List &eventList,, Queue &bankQueue) { 1. Delete the customer at the front of the bankQueue; 2. Delete the departure event from the eventList; 3. If (the bankQueue is not empty){ a. Insert a departure event in the eventList by letting eventStatus = ‘D’ and occurTime = departure time + the transaction time of the customer in front of the bankQueue. b. Sort eventList in ascending order. c. Calculating the total waiting time: waitingTime = departuretime – the arrival time of the customer in front of the bankQueue. (variable waitingTime can be delared outside of the main() function as global variable) } }
Join our real-time social learning platform and learn together with your friends!