Need help finishing the code below. Thanks in advance Modify or rewrite the DynI
ID: 3771565 • Letter: N
Question
Need help finishing the code below. Thanks in advance
Modify or rewrite the DynIntQueue class to simulate customer arrivals at the
Department of Motor Vehicles (DMV) counter. As customers arrive, they are
given a ticket number starting at 1 and incrementing with each new customer.
When a customer service agent is free, the customer with the next ticket is
called. This system results in a FIFO queue of customers ordered by ticket
number. Write a program that implements the queue and simulates customers
entering and leaving the queue. Input into the queue should be the ticket
number and a timestamp when the ticket was entered into the queue. A ticket
and its corresponding timestamp is removed when a customer service agent
handles the next customer. You should display the wait time for each
customer as they are removed from the queue. If nobody is in the queue,
output that the line is empty.
customerqueue.h
#ifndef CUSTOMERQUEUE_H
#define CUSTOMERQUEUE_H
class customerQueue {
private:
// Structure for the queue nodes
struct QueueNode {
int value; // Value in a node
QueueNode *next; // Pointer to the next node
};
QueueNode *front; // The front of the queue
QueueNode *rear; // The rear of the queue
int numCustomers; // Number of customers in the queue
public:
// Constructor
customerQueue();
// Destructor
~customerQueue();
// Queue operations
void enqueue(int);
int dequeue();
void displayQueue();
bool isEmpty() const;
int GetNumCustomers()
{
return numCustomers;
}
void clear();
};
#endif
customerQueue.cpp
#include <iostream>
#include "customerQueue.h"
#include <cstdlib>
using namespace std;
//Default constructor w/ empty queue
customerQueue::customerQueue()
{
front = 0;
rear= 0;
numCustomers=0;
}
//Destructor
customerQueue::~customerQueue()
{
cout << "In the destructor. ";
dequeue();
}
void customerQueue::enqueue(int num)
{
QueueNode *newNode;
newNode = new QueueNode;
newNode->value = num;
newNode->next = 0;
if(isEmpty())
{
front=newNode;
rear=newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
//update number of customers in line
numCustomers++;
}
int customerQueue::dequeue()
{
QueueNode *temp;
int num;
if(isEmpty())
{
cout << "The line is empty. ";
exit(EXIT_FAILURE);
}
else
{
num = front->value;
temp= front;
front = front->next;
delete temp;
numCustomers--;
}
return num;
}
bool customerQueue::isEmpty() const
{
bool status;
if(numCustomers > 0)
{
status = false;
}
else
{
status = true;
}
return status;
}
void customerQueue::clear()
{
while(!isEmpty() )
{
dequeue();
}
}
void customerQueue::displayQueue()
{
QueueNode * temp;
temp = front;
if(!temp)
{
cout << "Line is Empty";
}
else
{
do
{
cout << temp->value << " ";
temp = temp->next;
}
while(temp);
}
}
Explanation / Answer
1. /********************************* 2. Queue header file 3. ********************************/ 4. 5. #ifndef QUEUE_H 6. #define QUEUE_H 7. 8. #include <iostream> 9. using namespace std; 10. 11. class Queue 12. { 13. private: 14. class QueueNode 15. { 16. friend class Queue; 17. int value; 18. QueueNode *next; 19. QueueNode(int value1, QueueNode *next1 = NULL) 20. { 21. value = value1; 22. next = next1; 23. } 24. }; 25. QueueNode *front; 26. QueueNode *rear; 27. public: 28. Queue(); 29. ~Queue(); 30. 31. void enqueue(int); 32. void dequeue(int &); 33. bool isEmpty(); 34. void clear(); 35. 36. }; 37. 38. #endif 39. 40. /********************************** 41. Queue cpp 42. ***********************************/ 43. 44. #include <iostream> 45. #include <iomanip> 46. 47. #include "queue.h" 48. using namespace std; 49. 50. Queue::Queue () 51. { 52. front = NULL; 53. rear = NULL; 54. } 55. 56. Queue::~Queue() 57. { 58. clear(); 59. } 60. 61. void Queue::enqueue(int num) 62. { 63. if (isEmpty()) 64. { 65. front = new QueueNode(num); 66. rear = front; 67. } 68. } 69. 70. void Queue::dequeue(int &num) 71. { 72. QueueNode *temp; 73. if (isEmpty()) 74. { 75. cout<<"The queue is empty. "; 76. exit(1); 77. } 78. else 79. { 80. num = front->value; 81. temp=front; 82. front=front->next; 83. delete temp; 84. } 85. } 86. 87. bool Queue::isEmpty() 88. { 89. if (front == NULL) 90. return true; 91. else 92. return false; 93. } 94. 95. void Queue::clear() 96. { 97. int value; 98. while(!isEmpty()) 99. dequeue(value); 100. } 101. 102. /*************************** 103. main cpp 104. ***************************/ 105. 106. #include <iostream> 107. #include <iomanip> 108. using namespace std; 109. 110. #include <time.h> 111. #include "queue.h" 112. 113. int main() 114. { 115. int option; 116. Queue iQueue; 117. int num = 1; 118. 119. long seconds; 120. seconds = static_cast<long>(time(NULL)); 121. 122. cout<<"The line is empty"<<endl; 123. do { 124. cout<<"Enter '1' to simulate a customer's arrival, '2' to help the next customer, or '3' to quit"<<endl; 125. cin>>option; 126. 127. if (option == 1) 128. { 129. 130. cout<<"Customer "<< num <<" entered the queue at time "<<seconds<<endl; 131. iQueue.enqueue(num); 132. num++; 133. } 134. 135. else if (option == 2) 136. { 137. cout<<"Customer "<< num <<" is being helped at time "<<seconds<<endl; 138. //cout<<"Wait time = "<< <<"seconds "<<endl; 139. 140. } 141. 142. else 143. { 144. break; 145. } 146. 147. }while (option != 3); 148. 149. while (!iQueue.isEmpty()) 150. { 151. int value; 152. iQueue.dequeue(value); 153. cout<<value<<" "; 154. 155. } 156. 157. system("pause"); 158. return 0; 159. } /*Ticket Window is a simple program to show the implementaion of QUEUE. Program takes as input number of tickets required and ENQUEUE them. Only two tickets are allowed at one time.If more than two tickets are demanded, two are given and the remaining is enqueued again for its turn*/ #include <iostream.h> #include <conio.h> #include <process.h> class ticketqueue{ private: //Data Members int q[5]; int front, rear, noe; //noe..to store number of elements public: //Member Functions ticketqueue(){ noe = rear = front = 0;} //Constructor void enqueue(int); void ticketchecker(); void print(); int isempty(); void queuebuilder(); void menu(); }; ///////////////////// void ticketqueue :: enqueue(int a) { q[rear] = a; rear++; noe++; if (rear == 5) rear = 0; cout<<" "<<a<<" Is Enqeueued....."<<endl; } ///////////////////// void ticketqueue :: ticketchecker() { clrscr(); int not,rt; if ( !isempty() ) { not = q[front]; if (not > 2) { rt = not - 2; if ( rt > 0 ) { enqueue(rt); front++; cout<<""<<not<<"# Tickets were demanded. 2 Tickets aregiven. " <<"Enqueued again for next go to get remaining #"<<rt<<"Tickets"<<endl; } else if ( rt <= 0 ) front++; noe--; } else if ( not <= 2 ) { front++; noe--; cout<<""<<not<<"# tickets are given...."<<endl; } } else cout<<" !!! NO REQUEST IN THE QUEUE " <<endl; getch(); clrscr(); menu(); } ///////////////////// void ticketqueue :: print() { clrscr(); int i = front; if ( noe > 0 ) { cout<<" REQUESTS ENQUEUED ARE :" ; do { cout<<q[i]<<" "; i++; if ( i == 5 ) i = 0; } while ( i != rear ); } else cout<<" NO REQUEST IN QUEUE...QUEUE IS EMPTY "; getch(); clrscr(); menu(); } ////////////////// //Utility Fuction int ticketqueue :: isempty() { if ( noe == 0 ) return 1; else return 0; } ///////////////// //This Function takes input from the user and shows Queue graphically void ticketqueue::queuebuilder(){ clrscr(); int tickets, z = 0, i = 0, x = 20, y = 10, g = 1, h = 17; char c; cout<<"Enter number of tickets to purchase... "<<endl; while (!(c == 'E' || c == 'e')) { if ( z > 4) { clrscr(); cout<<" ERROR!!! Queue Size Violation...Exiting Now "<<endl; getch(); clrscr(); menu(); } else { cout<<" Enter : "; cin>>tickets; enqueue(tickets); z++; gotoxy(17,10); cout<<"->"; gotoxy(20,9); cout<<"----------------------------------"; gotoxy(x,y); cout<<q[i]; x+=2; gotoxy(20,11); cout<<"----------------------------------"; gotoxy(g,h); cout<<"PRESS C/c to CONTINUE E/e to END: "; cin>>c; h += 6; i++; } } getch(); clrscr(); menu(); } ////////////////// void ticketqueue::menu() { int b; gotoxy(15,5); cout<<"Press 1................ENTER NO OF TICKETS "; gotoxy(15,8); cout<<"Press 2................TICKET CHECKER "; gotoxy(15,11); cout<<"Press 3................ALL TICKET REQUESTS "; gotoxy(15,15); cout<<"Press 4................EXIT "; gotoxy(15,18); cout<<"NOW ENTER : "; cin>>b; if ( b == 1 ) queuebuilder(); else if ( b == 2 ) ticketchecker(); else if ( b == 3 ) print(); else if ( b == 4 ) exit(1); } ////////////////// void main() { clrscr(); ticketqueue t; t.menu(); getch(); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.