Question: 13.21//This is the header file queue.h. This is the interface for the class Queue, 2 //which is a class for a queue of symbols. 3
13.21//This is the header file queue.h. This is the interface for the class Queue,
2 //which is a class for a queue of symbols.
3 #ifndef QUEUE_H
4 #define QUEUE_H
5 namespace queuesavitch
6 {
7 struct QueueNode
8 {
9 char data;
10 QueueNode *link;
11 };
12 typedef QueueNode* QueueNodePtr;
13
14 class Queue
15 {
16 public:
17 Queue();
18 //Initializes the object to an empty queue.
19 Queue(const Queue& aQueue);
20 ~Queue();
21 void add(char item);
22 //Postcondition: item has been added to the back of the queue.
23 char remove();
24 //Precondition: The queue is not empty.
25 //Returns the item at the front of the queue and
26 //removes that item from the queue.
27 bool empty() const;
28 //Returns true if the queue is empty. Returns false otherwise.
29 private:
30 QueueNodePtr front; //Points to the head of a linked list.
31 //Items are removed at the head
32 QueueNodePtr back; //Points to the node at the other end of the
33 //linked list. Items are added at this end.
34 };
35 }//queuesavitch
36 #endif //QUEUE_H
13.22//Program to demonstrate use of the Queue class.
2 #include
3 #include "queue.h"
4 using namespace std;
5 using namespace queuesavitch;
6
7 int main()
8 {
9 Queue q;
10 char next, ans;
11
12 do
13 {
14 cout << "Enter a word: ";
15 cin.get(next);
16 while (next != ' ')
17 {
18 q.add(next);
19 cin.get(next);
20 }
21
22 cout << "You entered:: ";
23 while ( ! q.empty() )
24 cout << q.remove();
25 cout << endl;
26
27 cout << "Again?(y/n): ";
28 cin >> ans;
29 cin.ignore(10000, ' ');
30 } while (ans !='n' && ans != 'N');
31
32 return 0;
33
13.23/This is the implementation file queue.cpp.
2 //This is the implementation of the class Queue.
3 //The interface for the class Queue is in the header file queue.h.
4 #include
5 #include
6 #include
7 #include "queue.h"
8 using namespace std;
9
10 namespace queuesavitch
11 {
12 //Uses cstddef:
13 Queue::Queue() : front(NULL), back(NULL)
14 {
15 //Intentionally empty.
16 }
17
18 Queue::Queue(const Queue& aQueue)
19
(
Queue::~Queue()
22
23
24 //Uses cstddef:
25 bool Queue::empty() const
26 {
27 return (back == NULL); //front == NULL would also work
28 }
29
30 //Uses cstddef:
31 void Queue::add(char item)
32 {
33 if (empty())
34 {
35 front = new QueueNode;
36 front->data = item;
37 front->link = NULL;
38 back = front;
39 }
40
41 else
42 {
43 QueueNodePtr temp_ptr;
44 temp_ptr = new QueueNode;
45 temp_ptr->data = item;
46 temp_ptr->link = NULL;
47 back->link = temp_ptr;
48 back = temp_ptr;
49 }
50 }
51
52 //Uses cstdlib and iostream:
53 char Queue::remove()
54 {
55 if (empty())
56 {
57 cout << "Error: Removing an item from an empty queue. ";
58 exit(1);
59 }
60
61 char result = front->data;
62
63 QueueNodePtr discard;
64 discard = front;
65 front = front->link;
(
if (front == NULL) //if you removed the last node
67 back = NULL;
68
69 delete discard;
70
71 return result;
72 }
73 }//queuesavitch
Modify or rewrite the Queue class (Display 13.21 through 13.23) 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 number 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. Your program should save the length of time the last
three customers spent waiting in the queue. Every time a ticket is removed
from the queue, update these times and output the average of the last three
customers as an estimate of how long it will take until the next customer is
handled. If nobody is in the queue, output that the line is empty.
Code to compute a timestamp based on the computers clock is given
below. The time(NULL) function returns the number of seconds since
January 1, 1970, on most implementations of C++:
#include
...
int main()
{
long seconds;
seconds = static_cast(time(NULL));
cout << "Seconds since 1/1/1970: " << seconds << endl;
return 0;
}
Sample execution is shown here:
The line is empty.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
1
Customer 1 entered the queue at time 100000044.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
1
Customer 2 entered the queue at time 100000049.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
1
Customer 3 entered the queue at time 100000055.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
2
Customer 1 is being helped at time 100000069. Wait time = 25
seconds.
The estimated wait time for customer 2 is 25 seconds.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
2
Customer 2 is being helped at time 100000076. Wait time = 27
seconds.
The estimated wait time for customer 3 is 26 seconds.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
1
Customer 4 entered the queue at time 100000080.
Enter '1' to simulate a customer's arrival, '2' to help the
next customer, or '3' to quit.
2
Customer 3 is being helped at time 100000099. Wait time = 44
seconds.
The estimated wait time for customer 4 is 32 seconds.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
