0% found this document useful (0 votes)
105 views

Lecture 4 Data Structure Queue

Data model and algorithm

Uploaded by

Sello Hlabeli
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Lecture 4 Data Structure Queue

Data model and algorithm

Uploaded by

Sello Hlabeli
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Data Structure

Chapter 4
Queue

By: Hani
Introduction
Queue is a waiting list such as :
1. A line of persons waiting to check out at the
supermarket.
2. A line of vehicles at a toll booth.
3. A queue of planes waiting to land at an
airport.
4. A queue of jobs in a computer system waiting
for output device such as printer.
Introduction
The first item in the queue is the first to be
served.
A queue is a First-In-First-Out (FIFO) structure
First-Come-First-Served (FCFS) structure
Is a special kind of list in which the basic insert
and delete operations are restricted to the ends of
the list.
Items are removed from a queue at one end,
called front (head) of the queue, and elements are
added only at the other end called back (rear or
tail).
How Queue Works?

Items delete or remove Items add or insert


from here from here

Dequeue Queue Enqueue

Front Back
Basic Operation of Queue
1. Construct a queue
2. Check if Queue is empty
3. AddQ: Add an element at the back of the
queue
4. Front: retrieve the element at the front of
the queue
5. RemoveQ: Remove the element at the front
of the queue.
Enqueue & Dequeue

Enqueue : insert an element at the rear of the queue


Dequeue: remove an element from the front of the queue

Remove Insert
(Dequeue) front back (Enqueue)
So, a Queue object q as follows

q
0 1 2 3 4
myArray

Front Back

An item is added to the queue by storing it at position Back of the array,


Array-Based Implementation of Queue
myArray to store the elements of Queue

0 1 2 3 4 n
myArray ...

Front Back

Two integer variables


Front: The position in the array of the element that can be removed the position
Of the front queue element.
Back: The position in the array at which an element can be added the position
Of following the last queue element.
Queue Implementation of Array
There are several different algorithms to implement
Enqueue and Dequeue
Nave way
When enqueuing, the front index is always fixed and
the back index moves forward in the array.

back back back

3 3 6 3 6 9

front front front


Enqueue(3) Enqueue(6) Enqueue(9)
Queue Implementation of Array
Nave way (contd)
When dequeuing, the front index is fixed, and
the element at the front the queue is removed.
Move all the elements after it by one position.
(Inefficient!!!)
back back back = -1

6 9 9

front front front


Dequeue() Dequeue() Dequeue()
Queue Implementation of Array
A better way
When an item is enqueued, the back index moves
forward.
When an item is dequeued, the front index also
moves forward by one element
back

Enqueue(10) 10
front

back

Enqueue(20) 10 20
front

back

Enqueue(30) 10 20 30
front

back

Dequeue() 20 30
front

back

Dequeue() 30
front

The problem here is that the back index cannot move beyond the last element in the array.
Implementation using Circular Array

Using a circular array


When an element moves past the end of a circular array, it wraps
around to the beginning, e.g.
OOOOO7963 4OOOO7963 (after Enqueue(4))
After Enqueue(4), the back index moves from 3 to 4.
How to detect an empty or full queue, using a circular array
algorithm?
Use a counter of the number of elements in the queue.
Circular Array
Consider a queue for which QUEUE_CAPACITY=5 and
whose elements are integers. The sequence of operations
addQ 70, addQ 80, addQ50 produces the following
configuration:
0 1 2 3 4
70 80 50

Front = 0 Back = 3

Now suppose that two elements are removed


Back = 3

0 1 2 3 4
50

Front = 2

And that 90 and 60 are then added


Back = 5

0 1 2 3 4
50 90 60

Front = 2

Before another item can be inserted into queue, the elements


in the array must be shifted back to the beginning of the array
Back = 3

0 1 2 3 4
50 90 60

Front = 0
Recognizing Palindromes
A palindrome
A string of characters that reads the same from
left to right as its does from right to left
To recognize a palindrome, you can use a
queue in conjunction with a stack
A stack reverses the order of occurrences
A queue preserves the order of occurrences

19
Recognizing Palindromes
A nonrecursive recognition algorithm for
palindromes
As you traverse the character string from left to
right, insert each character into both a queue and
a stack
Compare the characters at the front of the queue
and the top of the stack

20
Recognizing Palindromes

Figure 7-3
The results of inserting a string into both a

queue and a stack

21
Queue Class
Attributes of Queue
front/back: front/back index
counter: number of elements in the queue
maxSize: capacity of the queue
values: point to an array which stores elements of the queue

Operations of Queue
IsEmpty: return true if queue is empty, return false otherwise
IsFull: return true if queue is full, return false otherwise
Enqueue: add an element to the rear of queue
Dequeue: delete the element at the front of queue
DisplayQueue: print all the data
Create Queue
Queue(int size = 10)
Allocate a queue array of size. By default, size = 10.
front is set to 0, pointing to the first element of the
array
rear is set to -1. The queue is empty initially.

Queue::Queue(int size /* = 10 */) {


values = new double[size];
maxSize = size;
front = 0;
back = -1;
counter = 0;
}
IsEmpty & IsFull
Since we keep track of the number of elements
that are actually in the queue: counter, it is
easy to check if the queue is empty or full.

bool Queue::IsEmpty() {
if (counter) return false;
else return true;
}
bool Queue::IsFull() {
if (counter < maxSize) return false;
else return true;
}
Queue.h
#include <iostream>

#ifndef QUEUE
#define QUEUE

const int QUEUE_CAPACITY = 128;


typedef AdditionProblem QueueElement;
class Queue {
public:
Queue();
bool empty() const;
void enqueue(const QueueElement & value);
void display(ostream & out) const;
QueueElement front() const;
void dequeue();
private:
QueueElement myArray[QUEUE_CAPACITY];
int Front, Back;
};
#endif
Constructor() & Empty()
Construct the queue

Queue::Queue() : Front(0), Back(0) { }

Check if the queue is empty

bool Queue::empty() const {


return (Front == Back);
}
Enqueue
bool Queue::Enqueue(double x) {
if (IsFull()) {
cout << "Error: the queue is full." <<endl;
return false;
}
else {
// calculate the new back position (circular)
back = (back + 1) % maxSize;
// insert new item
values[back] = x;
// update counter
counter++;
return true;
}
}
Dequeue
bool Queue::Dequeue(double & x) {
if (IsEmpty()) {
cout << "Error: the queue is empty." << endl;
return false;
}
else {
// retrieve the front item
x = values[front];
// move front
front = (front + 1) % maxSize;
// update counter
counter--;
return true;
}
}
Printing the elements
void Queue::DisplayQueue() {
cout << "front -->";
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << values[(front + i) % maxSize];
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
}
}
Main Program

int main(void) {
Queue queue(5);
cout << "Enqueue 5 items." << endl;
for (int x = 0; x < 5; x++)
queue.Enqueue(x);
cout << "Now attempting to enqueue again..." << endl;
queue.Enqueue(5);
queue.DisplayQueue();
double value;
queue.Dequeue(value);
cout << "Retrieved element = " << value << endl;
queue.DisplayQueue();
queue.Enqueue(7);
queue.DisplayQueue();
return 0;
}
Using Dynamic Array to Store Queue
Elements
Similar problems as with list and stack
Fixed size array can be specified
too large or too small
Dynamic array design allows sizing of
array for multiple situations
Results in structure as shown
myCapacity
determined
at run time

Nyhoff, ADTs, Data Structures and Problem


Solving with C++, Second Edition, 2005
31
Pearson Education, Inc. All rights reserved.
Queue.h Queue.cpp
Define the main functions of queue
Define the classes #include queue.h"

Public: Queue ()
------------ Empty ()
------------ Enqueue()
Front()
Private: Dequeue ()
------------
------------ Main program

3232

You might also like