KEMBAR78
Data structures and algorithms lab4 | PPTX
DATA STRUCTURES AND ALGORITHMS
LAB 4

Bianca Tesila
FILS, March 2014
OBJECTIVES
Queue
 Queue vs stack
 Applications

QUEUES


instance of an abstract data type (ADT) that
formalizes the concept of restricted collection
(FIFO = first in first out)
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
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?
QUEUES: WITH SIMPLE ARRAYS
QUEUES: WITH CIRCULAR ARRAYS
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.
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.
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

Data structures and algorithms lab4

  • 1.
    DATA STRUCTURES ANDALGORITHMS LAB 4 Bianca Tesila FILS, March 2014
  • 2.
    OBJECTIVES Queue  Queue vsstack  Applications 
  • 3.
    QUEUES  instance of anabstract data type (ADT) that formalizes the concept of restricted collection (FIFO = first in first out)
  • 4.
    QUEUES: BASIC OPERATIONS  enqueue(x)   dequeue()    Removesthe 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 astatic data structure (array, circular array) with a dynamic data structure (list) ‼Short reminder: Why do we use circular arrays instead of simple arrays?
  • 6.
  • 7.
  • 8.
    QUEUES VS STACKS ‼Exercise: Implementa 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 thelab 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