310 likes | 535 Vues
2. What is a Queue?. Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front).A queue is a FIFO
E N D
1. Queue Implementations IT221
24 Feb 06
2. 2 What is a Queue? Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front).
A queue is a FIFO “first in, first out” structure.
3. Using A Queue “Buffer” for printer uses a Queue
“Buffer” for a mail server uses a Queue
Operating System uses “Priority Queues”
Any problem requiring FIFO property should use a queue.
4. Queue ADT Operations MakeEmpty -- Sets queue to an empty state.
IsEmpty -- Determines whether the queue is currently empty.
IsFull -- Determines whether the queue is currently full.
Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue.
Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item.
5. ADT Queue Operations Transformers
MakeEmpty
Enqueue
Dequeue
Observers
IsEmpty
IsFull
6. template<class DataType>
class Queue
{
public:
Queue();
Queue(int);
~Queue();
bool isEmpty() const;
bool isFull()const;
void enqueue(DataType val);
DataType dequeue();
DataType peek();
private:
int front,rear,maxQue;
DataType* data;
};
7. Queue Implementation Two ways to implement queues
Linked List
Remove from front of list
Add to back of List
This is a bit more complicated than the stack implementation of adding and removing from front only
Arrays
Queue array has problem a stack array does not? Do you see what it is??
10. Do you catch my (right) drift?????????
11. Do you catch my (right) drift?????????
12. Demo program of Right Drift Problem The program available by copying
cp ~mckenna/IT221_06/WK7/24Feb/Qdemo/*.* .
Demonstrates this drift problem. Build and run the program and it shows what happens if a circular Q is not used. The ADT reports a “Full Q” even though only one element will be in it. This is a good practicum programming exercise to fix this right drift problem.
14. O(1) Dequeue? Can you think of a way to keep the Dequeue operation constant time vice O(N)?
15. It’s a wrap!
17. Overflow If the array is full, then you can’t enqueue.
(rear + 1)% maxQue) == front
18. Underflow(if the array is empty, then can’t delete)
19. Destructor A class member function with the same name as the class constructor except that the desructor has a tilde (~) in front of the name. ~classname( )
Implicitly invoked when a class object goes out of scope (destroyed).
20. Destructor What happens to dynamic memory when the pointer goes out of scope?
21. Destructor You may want to save the contents of a data structure to a file. The destructor can open a file and write the contents.
22. Array-based Queue has 2 constructors Queue(); // default
Queue(int); //overloaded constructor
Why do you think you would need 2 constructors?
23. Queue Class Using Dynamic Array template<class DataType>
class Queue
{
public:
Queue(); //default constructor
Queue(int); // set the size of the queue
~Queue(); // destructor
bool isEmpty() const; // True if empty
bool isFull() const; // True if full
void enqueue(DataType val); // put data in the queue
DataType dequeue(); // return first element and remove from the queue
DataType peek(); // return first element w/o removing from the queue
private:
int front, rear, maxQue; // index to front of Q, end of Q, and max elements in Q
DataType* data; // POINTER!! Will point to a dynamically allocated array
};
24. Implementation of Queue ADT The text (pg 225 -244) provides a few different ways to implement a queue class. Let’s take a look at one of those implementations that use a dynamically allocated array as the underlying data structure.