3. QUEUES
instance of an abstract data type (ADT) that
formalizes the concept of restricted collection
(FIFO = first in first out)
4. QUEUES: BASIC OPERATIONS
enqueue(x)
dequeue()
Removes the element from the head of the queue and
returns it
Returns an error if the stack is empty
peek()
Adds the element x at the tail of the queue
Returns (but does not remove) the element at the head of
the queue
isEmpty()
Returns 1 if the queue is empty and 0 otherwise
5. QUEUES: IMPLEMENTATION
with a static data structure (array, circular
array)
with a dynamic data structure (list)
‼Short reminder:
Why do we use circular arrays instead of simple arrays?
8. QUEUES VS STACKS
‼Exercise:
Implement a stack using 2 queues. The corresponding class is
QueuedStack and has the following methods:
an empty constructor
destructor
push
pop
There are several solutions for this task.
Using a similar approach, implement a queue using 2 stacks:
StackedQueue.
9. QUEUES AND STACKS: APPLICATIONS
‼Exercise:
Implement a stack which provides an efficient get minimum
function along with regular push and pop functionality.
Hint: use an auxiliary stack.
Taking into consideration that a queue can be implemented
using 2 stacks, use a similar approach for getting the
minimum element out of a queue.
10. HOMEWORK
o
Finish all the lab exercises.
o
Implement a messaging system using queues.
Messages are received in the order they are sent
You should use the following classes:
Message
MessageSender
MessageReceiver
A Message object has a sender, recipient, content (make an array
of chars!!) and a date
A Message is placed in a queue by a MessageSender object
A Message is removed from a queue by a MessageReceiver object,
which can also display the content of the message.
Your queue class can receive any types of objects, including
Message Objects