1 data structures csci 132, spring 2014 lecture 8 implementing queues
TRANSCRIPT
2
The Circular Model
The front and rear are the same as the linear model, except:The queue wraps around when the end of the array is reached.
append: add 1 to rear, if off the end of array, rear = 0; assign new item to entry[rear]
retrieve: return entry[front]serve: increment front by 1, if off the end of array, front = 0;
0 1 2 3 4 5 6 7 8 9
3 71
frontrear
8
3
Detecting an Empty Queue
The idea: in an empty queue, the front is behind the rear:front == rear + 1.
The problem: this is also true for a full array!
The possible solutions:1) Always leave an empty position. Then the queue is full when front == rear + 22) Use a boolean flag that is set to true when the queue becomes full.3) Use a counter to keep track of number of items in the queue.
4
Queue Operations
A Queue class should have the following methods:
append(item) //Add an item to the rear of the queue
serve( ) //Delete an item from the front of the queue
retrieve( ) //Return value of front item
empty() //Return true if queue is empty.
5
The Queue Specification
typedef int Queue_entry;
const int maxqueue =10 ; //small value for testing
class Queue {
public:
Queue();
bool empty()const;
Error_code serve();
Error_code retrieve(Queue_entry &item) const;
Error_code append(const Queue_entry &item);
private:
int count, front, rear;
Queue_entry entry [maxqueue ];
};
7
Implementing append( )Error_code Queue ::append(const Queue_entry &item)
{
Error_code outcome =success ;
if (count >=maxqueue) {
outcome =overflow ;
} else {
count++;
if ((rear + 1) == maxqueue) {
rear = 0;
} else {
rear = rear + 1;
}
entry[rear] = item;
}
return outcome ;
}
9
Implementing serve( )
Error_code Queue ::serve()
{
Error_code outcome =success ;
if (count <=0) {
outcome =underflow ;
} else {
count-- ;
front = ((front + 1) == maxqueue) ? 0 : (front + 1);
}
return outcome ;
}
11
Implementing retrieve( )
Error_code Queue ::retrieve(Queue_entry &item) const
{
if (count <=0){
return underflow ;
} else {
item =entry [front];
return success;
}
}
16
Inheritance
• is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class.
• The class being inherited from is the Base Class (Parent).
• The class that inherits is the Derived Class (Child).
• The derived class is then specialized by adding properties specific to it.
17
Inheritance Hierarchy among vehicles
vehicle
wheeled vehicle boat
bicyclecar
four-door two-door
Every car is a wheeled vehicle.
18
An extended queue class
class Extended_queue : public Queue {
public:
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
};
19
class Extended_queue:public Queue
• says class Queue is a public base class of the derived class Extended_queue.
• As a result, all public members of Queue (except constructors) are also public members of Extended_queue
• Extended_queue adds some functions that are not available in the Queue class.
20
Extended_queue inherits from Queue
methods: Queue append serve retrieve emptydata members: count front rear entry[ ]
methods: Extended_queue append serve retrieve empty size clear full serve_and_retrievedata members: count front rear entry[ ]
class Queue class Extended_queue
Inherited
21
private vs. protected
The derived class does not have access to private data members of the parent class.
The derived class does have access to protected data members of the parent class.
The client code does not have access to either private or protected data members.
22
A new Queue specification
const int maxqueue =10 ; //small value for testing
class Queue {
public:
Queue();
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 ];
};