Data Structures
CSCI 132, Spring 2014
Lecture 9
Queue Applications
1
The Queue Specification
typedef int Queue_entry;
const int maxqueue =10 ;
class Queue {
public:
Queue();
Queue(Queue_entry item);
Queue (Queue_entry item) {
count = 1;
front = 0;
rear = 0;
entry[front] = item;
} //set first value of queue
//not normally in Queue class.
//Just for demo purposes.
bool empty()const;
Error_code serve();
Error_code retrieve(Queue_entry &item) const;
Error_code append(const Queue_entry &item);
protected:
int count, front, rear;
Queue_entry entry [maxqueue ];
};
2
An extended queue class
class Extended_queue : public Queue {
public:
Extended_queue();
//constructor
Extended_queue(Queue_entry item); //Another constructor
bool full()const;
//return true if queue is full
int size() const;
//return number of items in queue
void clear();
//make queue empty
Error_code serve_and_retrieve(Queue_entry &item); //return
// value at front of queue and delete the item from the queue
private:
bool isFull;
};
3
Constructor Rules for the
Derived Class
•
•
At run time, the base class constructor is
implicitly called first, before the body of the
derived class’s constructor executes.
If the base class constructor requires
parameters, they must be passed by the
derived class’s constructor.
4
Extended_Queue Default
Constructor
Extended_queue :: Extended_queue ( )
{
// front, rear and count are initialized by implicit call to
// Queue( ) default constructor
isFull = false;
//initialize Extended_queue data
}
5
Another Extended_Queue
Constructor
Extended_queue :: Extended_queue ( Queue_entry item)
: Queue (item) // constructor initializer
{
// front, rear and count are initialized by explicit call to
// Queue(item ) constructor above
isFull = false;
}
6
Calling a Queue function
Error_code Extended_queue :: serve_and_retrieve ( Queue_entry &item) {
Error_code outcome;
outcome = Queue :: retrieve(item); //only need scope resolution if function
//is overloaded
Queue :: serve( );
return outcome;
}
//OR—if there are no function overloads:
Error_code Extended_queue :: serve_and_retrieve ( Queue_entry &item) {
Error_code outcome;
outcome = retrieve(item);
serve( );
return outcome;
}
7
A Queue Application
Write an application to process ticket order and sales:
•Program will be able to input requests to buy tickets and
requests to sell tickets.
•If no tickets are available, requests to buy tickets will be
put in a queue to wait for availability.
•If there are no ticket requests, offers to sell tickets will
be put in a queue to wait for a request to buy.
•Requests will be matched up with offers on a first
come-first served basis.
8
Storing information in Queues
We might want to store multiple pieces of information about
our buyers and sellers in the Queues. A struct is useful for
this:
struct Person {
char Name[30];
char address[100];
char phone[15];
};
typedef Person Queue_entry;
9
Implementing main( )
int main() {
char command;
Queue ticketRequests, ticketsForSale;
cout << "Enter 'R' to request tickets. Enter 'S' to sell tickets." << endl;
cout << "End with 'Q'" << endl;
command = get_command();
while (command != 'q') {
if (command == 'r'){
request_tickets(ticketRequests, ticketsForSale);
} else {
sell_tickets(ticketRequests, ticketsForSale);
}
command = get_command();
}
cout << "Thank you for visiting the ticket center!" << endl;
return 0;
}
10
Implementing get_command( )
char get_command(void) {
char command;
cout << "Enter a command:" << endl;
cin >> command;
command = tolower(command);
while ((command != 'r') && (command != 's') && (command != 'q')) {
cout << "Invalid command: '" << command << "'" << endl;
cout << "Enter 'R' to request tickets. Enter 'S' to sell tickets." << endl;
cout << "End with 'Q'" << endl;
cout << "Enter a command:" << endl;
cin >> command;
command = tolower(command);
}
return command;
}
11
Implementing request_tickets( )
void request_tickets(Queue &requestQueue, Queue &forSaleQueue) {
Person requester, seller;
cout << "Enter name of requester: " << endl;
}
12
Implementing request_tickets( )
void request_tickets(Queue &requestQueue, Queue &forSaleQueue) {
Person requester, seller;
cout << "Enter name of requester: " << endl;
cin >> requester.Name;
if (forSaleQueue.empty()) {
if (requestQueue.append(requester) == overflow) {
cout << "Request list full. Try again later." << endl;
} else {
cout << requester.Name << " has requested tickets for the concert." << endl;
}
} else {
forSaleQueue.retrieve(seller);
forSaleQueue.serve();
cout << requester.Name << " has bought tickets from " << seller.Name << endl;
}
cout << endl;
}
13
Implementing sell_tickets( )
void sell_tickets(Queue &requestQueue, Queue &forSaleQueue) {
Person requester, seller;
cout << "Enter seller's name:" << endl;
}
14
Implementing sell_tickets( )
void sell_tickets(Queue &requestQueue, Queue &forSaleQueue) {
Person requester, seller;
cout << "Enter seller's name:" << endl;
cin >> seller.Name;
if (requestQueue.empty() ) {
if (forSaleQueue.append(seller) == overflow) {
cout << "Sale list full. Try again later." << endl;
} else {
cout << seller.Name << " has been added to the list of ticket sellers." << endl;
}
} else {
requestQueue.retrieve(requester);
requestQueue.serve();
cout << requester.Name << " has bought tickets from " << seller.Name << endl;
}
cout << endl;
}
15
© Copyright 2026 Paperzz