Top Banner
Queues Chapter 3
31

Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Dec 30, 2015

Download

Documents

Robyn Lindsey
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queues

Chapter 3

Page 2: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Objectives

• Introduce the queue abstract data type.– Queue methods– FIFO structures

• Discuss inheritance in object oriented programming.– Extending a class

• Implement contiguous queues.– Circular arrays

• Introduce the switch statement.

Page 3: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Homework Overview

• Written (max 10 points)– 3.1 E1 (a,b,c) (2 pts each)– 3.1 E2 (a,b) (4 pts)

• Programming (max 22 points)– 3.3 E4 (8 pts)– 3.3 P1 (10 pts)– 3.4 P1 (8 pts)– 12 Days (10 pts)

• Group Project (12 points)– 3.3 E2 (a,b,c) (12 pts)

Page 4: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queues

• Queue – think about people standing in line.

• We add items at one end (the rear) and remove items at the other (the front).

• This is a “first-in, first-out” (FIFO) data structure.• C++ contains support for a queue container adaptor

(#include <queue>).

Page 5: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Operations

A Queue must support the following operations:• Create an empty queue.• Append – add a new entry at the rear.• Serve – remove the entry at the front.• Retrieve – get the value of the entry at

the front.• Empty – determine if the queue is empty.

Page 6: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Class

class Queue {public:

Queue(); bool empty() const;Error_code serve();Error_code append(const Queue_entry &item);Error_code retrieve(Queue_entry &item) const;

private:…};

• We will use the same Error_code enumeration as before.

Page 7: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Extending the Queue Class

• We may want to add more than the required methods to our abstract data types.

• For instance we might want to add the following to the queue type.– Full – is the queue full?– Size – How many items are in the queue?– Clear – Empty the queue of all its entries.– Serve-and-retrieve – combine the methods of

retrieve and serve.

Page 8: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Extending a Class

• We may want to add features to a previously defined class.

• C++ supports this using inheritance.– We define a new class that uses a old one in its definition.

class Extended_queue: public Queue{public:

bool full() const;int size() const;void clear();Error_code serve_and_retrieve(Queue_entry & item);

};

• An extended queue is a queue, just with new features added.

Page 9: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Homework – Section 3.1 (page 84)

• Written– E1 (a, b, c) (written on paper) (2 pts each)– E2 (a, b) (written on paper) (4 pts)

Page 10: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Implementing Queues

• How do we implement a queue?• We could use an array were the front is fixed at index 0.

• Appending an entry is easy.

• Serving an entry is not.

Page 11: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Implementing Queues

• Another option would be to allow the front to move in the array.

• Now serving an entry is easy.

• The problem is that we just “lost” a slot in the array. – If we do this too many times we will run out of room in

the array.

Page 12: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Circular Array

• To fix this, when we reach the end of the array we can wrap back around to index 0 to append a new item.

• This will solve the problem but we will need to code this carefully.

Page 13: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Circular Array

• It can be helpful to think of the array as a circle.

Page 14: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Front and Rear

• The relationship between front and rear can now be ambiguous.

• When a queue is full rear = front – 1.

• When there is one entry rear = front.

Page 15: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Front and Rear

• When the last item is served, the front moves one space to the right. Now rear = front - 1 again.

• A full queue and an empty queue look the same!

• It will be important to have a way to remember if the queue is full or empty.

Page 16: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Math

• Suppose max is the number of entries in the array.– The array is indexed from 0 to max - 1.

• We can use modular arithmetic to perform the wrapping.

• Serve a entry:front = (front + 1) % max;

• Check to see if the queue is full (or empty):front == (rear + 1) % max;

Page 17: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Math

• This can also be done using the ternary operator.

front = ((front+1) == max) ? 0 : (front +1);

• It can also be done using an if-else.if((front+1) == max) front = 0; else front = front + 1;

Page 18: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Classclass Queue {public:

Queue(); // constructorbool empty() const;/* Post: If the Queue is empty, true is returned. Otherwise false is

returned. */

Error_code serve();/* Post: The front of the Queue is removed. If the Queue is empty return an Error_code underflow. */

Error_code append(const Queue_entry &item);/* Post: item is added to the rear of the Queue. If the Queue is full

return an Error_code overflow and leave the Queue unchanged. */

Error_code retrieve(Queue_entry &item) const;/* Post: The front fo the Queue retrieved to the output parameter item. If

the Queue is empty return an Error_code of underflow. */

protected:int count;int front, rear;Queue_entry entry[maxqueue];

};

Page 19: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Class

• Notice that the data is protected instead of private.– This will allow us to extend the queue class

and have the methods of the new class directly access the data.

• We will use the count variable to tell if the queue is full or empty.

Page 20: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue CodeError_code Queue::append(const Queue_entry &item)/* Post: item is added to the rear of the Queue. If the Queue is full return an

Error_code overflow and leave the Queue unchanged. */{

if (count >= maxqueue) return overflow;count++;rear = ((rear + 1) == maxqueue) ? 0: (rear + 1);entry [rear] = item;return success;

}

Error_code Queue::serve()/* Post: The front of the Queue is removed. If the Queue is empty return an

Error_code underflow. */

{if (count <= 0) return underflow;count--;front = ((front + 1) == maxqueue) ? 0: (front + 1);return success;

}

Page 21: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue CodeError_code Queue::retrieve(Queue_entry &item) const/* Post: The front fo the Queue retrieved to the output parameter item. If the

Queue is empty return an Error_code of underflow. */{

if (count <= 0) return underflow;item = entry[front];return success;

}

bool Queue::empty() const/* Post: If the Queue is empty, true is returned. Otherwise false is returned. */{

return count == 0;}

Queue::Queue()/* Post: The Queue is initialized to be empty.*/{

count = 0;rear = maxqueue - 1;front = 0;

}

Page 22: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Extending the Queue Class

• The methods of the extended queue class will look like the following.

int Extended_queue::size() const/* Post: Return the number of entries in the Extended_queue. */{

return count;}

• Notice that the method can directly access count since it was protected in the original Queue class.

• This would not work if count were private.

Page 23: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Homework – Section 3.3 (page 91)

• Written– E4 (email code and be sure to test it) (8 pts)

• Programming – P1 (email code) (10 pts)

• Group Project• E2 (a, b, c) (test and email code) (12 pts)

Page 24: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Testing Codevoid help()/* Post: A help screen for the program is printed, giving the meaning of each command that the user may enter.*/{

cout << endl << "This program allows the user to enter one command" << endl

<< "(but only one) on each input line." << endl << "For example, if the command S is entered, then" << endl << "the program will serve the front of the queue." << endl << endl << "The valid commands are:" << endl << "A - Append the next input character to the queue" << endl << "S - Serve the front of the queue" << endl << "R - Retrieve and print the front entry" << endl << "H - This help screen" << endl << "Q - Quit" << endl << "Press < Enter > to continue." << flush;

char c;cin.get(c); // flush out an extra newlinedo { cin.get(c);} while (c != '\n');

}

Page 25: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Testing Code

char get_command()/* Post: The user enters a command (A, S, R, H, Q, a, s, r, h, or q). The function returns the command entered. */{

char command;bool waiting = true;cout << "Select command and press < Enter > :";while (waiting){ cin >> command;command = tolower(command);if (command == 'a' || command == 's' || command == 'r' || command == 'h' || command == 'q') waiting = false; else {cout << "Please enter a valid command:" << endl << "[A]append [S]serve" << endl << "[R]retrieve [H]Help" << endl << "[Q]uit." << endl;}}return command;

}

Page 26: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Testing Code

bool do_command(char c, Queue &test_queue)/* Pre: c represents a valid command. Post: Performs the given command c on the Queue test_queue.

Returns false if c == 'q', otherwise returns true. Uses: The class Queue. */{

bool continue_input = true;Queue_entry x;switch (c) {case 'r':

if (test_queue.retrieve(x) == underflow)cout << "Queue is empty." << endl;

elsecout << endl

<< "The first entry is: " << x << endl;

break;case 'a':

cout << "Please enter a character to add to the queue. " << flush;cin >> x;if (test_queue.append(x) == overflow)

cout << "Queue is full." << endl;break;

Page 27: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Queue Testing Code

case 's':if (test_queue.serve() == underflow)

cout << "Queue is empty." << endl;break;

case 'h':help();break;

case 'q':cout << "Queue demonstration finished." << endl;continue_input = false;break;

}return continue_input;

}

Page 28: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Switch Statement

• Notice the use of the switch statement.– This does the same job as a series of nested if-else statements.

• The syntax for the switch is switch (expression){ case constant1: group-of-statements-1; break; case constant2: group-of-statements-2; break; . . . default: default-group-of-statements}

Page 29: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Switch Statement

The switch statement works in the following manner:1. The expression is evaluated.2. The resulting value is compared to constant1.– If they are equal it executes group-of-statements-1 until it

finds the break statement. – When it finds this break statement, the program jumps to

the end of the entire switch statement (the closing brace).

3. If expression was not equal to constant1, it is checked against constant2. If it is equal, then it executes group-of-statements-2 until a break is found.

4. This repeats with each value.5. If the value of expression did not match any of the

specified constants, the program executes the statements included after the default label.

Page 30: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Switch Statement

• One common mistake is to forget the break statements.– The program will continue executing the code for the

subsequent cases.– It only stops when it reaches a break.– This is called a “fall-through” structure.– This is a “feature” that can be exploited in some situations.

• If there is nothing to do in the default case, the default may be omitted.

• The expression in a switch statement should evaluate to some enumerated type (not float, double, etc.).

Page 31: Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.

Homework – Section 3.4 (page 93)

• Programming– P1 (Modify the queue testing program to test

the Extended_queue class methods.)(email code) (8 pts)

– Write a program that exploits the fall-through feature of the switch statement to print out the lyrics to the 12 Days of Christmas. Use as few output statements as possible. (email code) (10 pts) http://www.metrolyrics.com/12-days-of-christmas-lyrics-christmas-song.html