Data structures and algorithms lab4

2,074 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,074
On SlideShare
0
From Embeds
0
Number of Embeds
1,296
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Data structures and algorithms lab4

  1. 1. DATA STRUCTURES AND ALGORITHMS LAB 4 Bianca Tesila FILS, March 2014
  2. 2. OBJECTIVES Queue  Queue vs stack  Applications 
  3. 3. QUEUES  instance of an abstract data type (ADT) that formalizes the concept of restricted collection (FIFO = first in first out)
  4. 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. 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?
  6. 6. QUEUES: WITH SIMPLE ARRAYS
  7. 7. QUEUES: WITH CIRCULAR ARRAYS
  8. 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. 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. 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

×