Title: Data Structures
1Data Structures
Andreas Savva
2Queues
- 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)
3Application of Queuesin our everyday life
4Application 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
5Basic 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.
6The Class Queue
class Queue
methods Queue (constructor) clear
empty full size retrieve serve
retrieve_and_serve append print Data
members
7Stack 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
8Data Structure - Array
- front is at position 0
- rear is at position n-1
front
rear
One item
front
rear
n items
9Create Queue
We use a constructor to initialize a new created
queue as empty.
- QueueQueue()
- // Pre None.
- // Post Initialize Queue to be empty.
-
entry
10Clear
void Queueclear() // Pre None. // Post All
entries in the Queue have been // removed it is
now empty.
entry
11Empty
bool Queueempty() const // Pre None. //
Post Return true if the Queue is
empty, // otherwise false is returned.
entry
12Full
bool Queuefull() const // Pre None. //
Post Returns true if the Queue is
full, // otherwise false is returned.
entry
13Size
int Queuesize() const // Pre None. //
Post Return the number of entries in the
Queue.
entry
14Retrieve
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
15Serve 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
16Retrieve 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
17Append 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
18Print
void Queueprint() const // Pre None. //
Post Display the entries of the Queue.
19Data Structure Circular Array
20Queue containing one item
front
rear
Empty Queue
-1
front
rear
Queue with one empty position
front
rear
Full Queue
front
rear
21The 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
22Circular Queue
23Create Queue
24Clear
void Queueclear()
rear-1
25Empty
bool Queueempty() const
Not Empty
Empty
rear-1
26Next position
rear
int Queuenext(int n) const
front next(front) rear next(rear) entryrear
front
27Full
bool Queuefull() const
NOT FULL
FULL
28Size
int Queuesize() const
max
rear front 1
29Retrieve
Error_code Queueretrieve(Queue_entry item)
const
30Serveor Dequeue
Error_code Queueserve()
31RetrieveandServe
Error_code Queueretrieve_and_serve(Queue_entry
item)
32AppendorEnqueue
rear
Error_code Queueappend(const Queue_entry
item)
33Print
void Queueprint() const