Data Structures - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Data Structures

Description:

Andreas Savva Data Structures Chapter 3 Queues – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 34
Provided by: Andreas229
Category:

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures
Andreas Savva
  • Chapter 3
  • Queues

2
Queues
  • A data structure modeled after a line of people
    waiting to be served.
  • The first entry which is inserted is the first
    one that will be removed. (First In First Out
    FIFO)

3
Application of Queuesin our everyday life
4
Application of QueuesMost common than Stacks
  • Waiting for a printer
  • Access to disk storage
  • In a time-sharing system use of the CPU
  • Along with stacks, queues are one of the simplest
    data structures

5
Basic Operations for Queues
  • Create a queue, leaving it empty.
  • Clear the queue leaving it empty.
  • Test whether the queue is Empty.
  • Test whether the queue is Full.
  • Return the Size of the queue.
  • Retrieve the first entry of the queue, provided
    the queue is not empty.
  • Serve remove the first entry from the queue,
    provided the queue is not empty.
  • Retrieve and Serve retrieves and remove the
    first entry from the queue, provided the queue is
    not empty.
  • Append a new entry at the end of the queue,
    provided that the queue is not full.
  • Print all the entries of the queue.

6
The Class Queue
class Queue
methods Queue (constructor) clear
empty full size retrieve serve
retrieve_and_serve append print Data
members
7
Stack implementation - Array
  • typedef double Queue_entry
  • enum Error_code success,overflow,underflow
  • const int max 100
  • class Queue
  • public
  • Queue()
  • void clear()
  • bool empty() const
  • bool full() const
  • int size() const
  • Error_code retrieve(Queue_entry ) const
  • Error_code serve()
  • Error_code retrieve_and_serve(Queue_entry )
  • Error_code append(const Queue_entry )
  • void print() const
  • private
  • int count
  • Queue_entry entrymax

8
Data Structure - Array
  • front is at position 0
  • rear is at position n-1

front
rear
One item
front
rear
n items
9
Create Queue
We use a constructor to initialize a new created
queue as empty.
  • QueueQueue()
  • // Pre None.
  • // Post Initialize Queue to be empty.

entry
10
Clear
void Queueclear() // Pre None. // Post All
entries in the Queue have been // removed it is
now empty.
entry
11
Empty
bool Queueempty() const // Pre None. //
Post Return true if the Queue is
empty, // otherwise false is returned.
entry
12
Full
bool Queuefull() const // Pre None. //
Post Returns true if the Queue is
full, // otherwise false is returned.
entry
13
Size
int Queuesize() const // Pre None. //
Post Return the number of entries in the
Queue.
entry
14
Retrieve
Error_code Queueretrieve(Queue_entry item)
const // Pre None. // Post If the Queue is not
empty, the front of the queue is recorded // as
item. Otherwise an Error_code of underflow is
return.
entry
15
Serve or Dequeue
Error_code Queueserve() // Pre None. //
Post If the Queue is not empty, the front of the
Queue is removed // Otherwise an Error_code of
underflow is returned.
count
entry
16
Retrieve and Serve
Error_code Queueretrieve_and_serve(Queue_entry
item) // Pre None. // Post If the Queue is not
empty, the front of the queue is removed
and // recorded as item. Otherwise an Error_code
of underflow is return.
item
entry
A
17
Append or Enqueue
Error_code Queueappend(const Queue_entry
item) // Pre None. // Post If the Queue is not
full, item is added at the end of the
Queue. // If the Queue is full, an Error_code of
overflow is returned // and the Queue is left
unchanged.
entry

n1
18
Print
void Queueprint() const // Pre None. //
Post Display the entries of the Queue.
19
Data Structure Circular Array
20
Queue containing one item
front
rear
Empty Queue
-1
front
rear
Queue with one empty position
front
rear
Full Queue
front
rear
21
The class Queue Circular Array
  • class Queue
  • public
  • Queue()
  • void clear()
  • bool empty() const
  • bool full() const
  • int size() const
  • Error_code retrieve(Queue_entry ) const
  • Error_code serve()
  • Error_code retrieve_and_serve(Queue_entry )
  • Error_code append(const Queue_entry )
  • void print() const
  • private
  • int next(int n) const
  • int front, rear
  • Queue_entry entrymax

22
Circular Queue
23
Create Queue
  • QueueQueue()

24
Clear
void Queueclear()
rear-1
25
Empty
bool Queueempty() const
Not Empty
Empty
rear-1
26
Next position
rear
int Queuenext(int n) const



front next(front) rear next(rear) entryrear

front
27
Full
bool Queuefull() const

NOT FULL
FULL

28
Size
int Queuesize() const
max
rear front 1
29
Retrieve
Error_code Queueretrieve(Queue_entry item)
const
30
Serveor Dequeue

Error_code Queueserve()
31
RetrieveandServe

Error_code Queueretrieve_and_serve(Queue_entry
item)
32
AppendorEnqueue
rear

Error_code Queueappend(const Queue_entry
item)
33
Print
void Queueprint() const
Write a Comment
User Comments (0)
About PowerShow.com