Sei sulla pagina 1di 15

Data Structures

CSCI 132, Spring 2013 Lecture 9


Queue Applications

The Queue Specification


typedef int Queue_entry; const int maxqueue =10 ; class Queue { public: Queue(); Queue(int init_count); //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 ]; };
Queue (int init_ct) { count = init_ct; front = 0; if (init_ct == 0) rear = maxqueue - 1; else rear = front + init_ct -1; } //but no values filled in

An extended queue class


class Extended_queue : public Queue { public: Extended_queue(); //constructor Extended_queue(int init_count); //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 classs constructor executes. If the base class constructor requires parameters, they must be passed by the derived classs constructor.

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

Another Extended_Queue Constructor


Extended_queue :: Extended_queue ( int init_count) : Queue (init_count) // constructor initializer { // front, rear and count are initialized by explicit call to // Queue(init_count ) constructor above isFull = false; }

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; } //ORif there are no function overloads: Error_code Extended_queue :: serve_and_retrieve ( Queue_entry &item) { Error_code outcome; outcome = retrieve(item); serve( ); return outcome; }

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

Potrebbero piacerti anche