140 likes | 159 Views
Queues. Lecture 30 Mon, Apr 9, 2007. Queues. A queue is a List that operates under the principle “first in, first out” ( FIFO ). New elements are enqueued into the queue. Old elements are dequeued from the queue.
E N D
Queues Lecture 30 Mon, Apr 9, 2007
Queues • A queue is a List that operates under the principle “first in, first out” (FIFO). • New elements are enqueued into the queue. • Old elements are dequeued from the queue. • To enforce the FIFO principle, we enqueue and dequeue at opposite ends. Queues
Implementation of Queues • Implement a Queue as a subclass of a List class. • Use pushFront() and popBack(), or • Use pushBack() and popFront(). • Choose a List class for which enqueuing and dequeuing will be efficient. Queues
Private Inheritance • Private inheritance prevents violations of the FIFO principle. • The Queue class has access to all public and protected List functions. • The Queue class user has access only to the queue functions. Queues
Queue Constructors • Construct an empty queue. • Construct a copy of the specified queue. Queue(); Queue(const Queue& q); Queues
Inspectors • Get a copy of the element at the head of the queue. • Get the number of elements in the queue. • Determine whether the queue is empty. T head() const; int size() const; bool isEmpty() const; Queues
Mutators • Enqueue the specified value at the tail of the queue. • Dequeue and return the element at the head of the queue. • Make the queue empty. void enqueue(const T& value); T dequeue(); void makeEmpty(); Queues
Facilitators • Read a queue from the specified input stream. • Write a queue to the specified output stream. void input(istream& in); void output(ostream& out) const; Queues
Other Member Functions • Determine whether the queue has a valid structure. void isValid() const; Queues
Non-Member Functions • Read a queue from the specified input stream. • Write a queue to the specified output stream. istream& operator>>(istream& in, Queue& q); ostream& operator<<(ostream& out, const Queue& q); Queues
Queue Implementation • Choose an appropriate List class as a base class. • Good choices • CircArrayList • LinkedListwTail • DoublyLinkedList • CircLinkedList Queues
Queue Implementation • Bad choices • ArrayList • LinkedList Queues