Lecture 4 Data Structure Queue
Lecture 4 Data Structure Queue
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?
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
Remove Insert
(Dequeue) front back (Enqueue)
So, a Queue object q as follows
q
0 1 2 3 4
myArray
Front Back
0 1 2 3 4 n
myArray ...
Front Back
3 3 6 3 6 9
6 9 9
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
Front = 0 Back = 3
0 1 2 3 4
50
Front = 2
0 1 2 3 4
50 90 60
Front = 2
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
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.
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
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
Public: Queue ()
------------ Empty ()
------------ Enqueue()
Front()
Private: Dequeue ()
------------
------------ Main program
3232