Queues in C++ detailed explanation and examples .ppt

Jamiluddin39 18 views 52 slides Dec 19, 2024
Slide 1
Slide 1 of 52
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52

About This Presentation

Queues in C++ with explanation


Slide Content

Queues
By Mansoor Ahmad

What is a queue?
•It is an ordered group of homogeneous items of
elements.
•Queues have two ends:
–Elements are added at one end.
–Elements are removed from the other end.
•The element added first is also removed first
(FIFO: First In, First Out).

Queue Specification
•Definitions: (provided by the user)
–MAX_ITEMS: Max number of items that might be on
the queue
–ItemType: Data type of the items on the queue
•Operations
–MakeEmpty
–Boolean IsEmpty
–Boolean IsFull
–Enqueue (ItemType newItem)
–Dequeue (ItemType& item)

Implementing Queue
Using linked List:
front
2571
1 7 5 2
frontrear
rear

Implementing Queue
Using linked List:
front
2571
1 7 5 2
frontrear
rear
front
257
1 7 5 2
frontrear
rear
dequeue()

Implementing Queue
Using linked List:
front
2571
1 7 5 2
frontrear
rear
front
257
97 5 2
frontrear
rear
enqueue(9)
9

Implementing Queue
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}

Implementing Queue
int front()
{
return front->get();
}
int isEmpty()
{
return ( front == NULL );
}

Queue using Linked List

Queue using Linked List

Queue using Linked List

Queue using Linked List Ex 2

Queue using Linked List Ex 2

Queue using Array
If we use an array to hold queue elements,
both insertions and removal at the front
(start) of the array are expensive.
This is because we may have to shift up to
“n” elements.
For the stack, we needed only one end; for
queue we need both.
To get around this, we will not shift upon
removal of an element.

Queue using Array

front
2571
rear
65 7
0
01 32 4
front
17 52
3
rear

Queue using Array

front
2571
rear
65 7
0
01 32 4
front
17 52
4
rear
enqueue(6)
6
6

Queue using Array

front
2571
rear
65 7
0
01 32 4
front
17 52
5
rear
enqueue(8)
6
6
8
8

Queue using Array

front
257
rear
65 7
1
01 32 4
front
7 52
5
rear
dequeue()
6
6
8
8

Queue using Array

front
25
rear
65 7
2
01 32 4
front
52
5
rear
dequeue()
6
6
8
8

Queue using Array

front
25
rear
65 7
2
01 32 4
front
52
7
rear
enqueue(9)
enqueue(12)
6
6
8
8
9
9
12
12
enqueue(21) ??

Queue using Array
We have inserts and removal running in
constant time but we created a new
problem.
Cannot insert new elements even though
there are two places available at the start
of the array.
Solution: allow the queue to “wrap
around”.

Queue using Array
Basic idea is to picture the array as a
circular array.
front
25
rear
2
front
7
rear
68912
6
5
7
0 1
3
2
4
5
2
68
9
12

Queue using Array
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
front
25
rear
2
front
0
rear
68912
6
5
7
0 1
3
2
4
5
2
68
9
12
enqueue(21)
21
21
8
size
7
noElements

Queue using Array
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
front
25
rear
2
front
1
rear
68912
6
5
7
0 1
3
2
4
5
2
68
9
12
enqueue(7)
21
21
8
size
8
noElements
7
7

Queue using Array
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
front rear
4
front
1
rear
68912
6
5
7
0 1
3
2
4
68
9
12
dequeue()
21
21
8
size
6
noElements
7
7

Code of Queue using Array

Code of Queue using Array

Code of Queue using Array Ex2

Code of Queue using Array Ex2

Implementation issues
•Implement the queue as a circular
structure.
•How do we know if a queue is full or
empty?
•Initialization of front and rear.
•Testing for a full or empty queue.

Make front point to the element preceding the front
element in the queue (one memory location will be
wasted).

Initialize front and rear

Queue is empty
now!!
rear == front

Queue Implementation
template<class ItemType>
class QueueType {
public:
QueueType(int);
QueueType();
~QueueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);

private:
int front;
int rear;
ItemType* items;
int maxQue;
};

Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::QueueType(int
max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}

Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items;
}

Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::
MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}

Queue Implementation (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return (rear == front);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
return ( (rear + 1) % maxQue == front);
}

Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Enqueue
(ItemType newItem)
{
rear = (rear + 1) % maxQue;
items[rear] = newItem;
}

Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
item = items[front];
}

Queue overflow
•The condition resulting from trying to add
an element onto a full queue.
 
if(!q.IsFull())
q.Enqueue(item);

Queue underflow
•The condition resulting from trying to
remove an element from an empty queue.
 
if(!q.IsEmpty())
q.Dequeue(item);

Example: recognizing palindromes
•A palindrome is a string that reads the same
forward and backward.
Able was I ere I saw Elba
 
•We will read the line of text into both a stack
and a queue.
•Compare the contents of the stack and the
queue character-by-character to see if they
would produce the same string of characters.

Example: recognizing palindromes

Example: recognizing palindromes
#include <iostream.h>
#include <ctype.h>
#include "stack.h"
#include "queue.h“
int main()
{
StackType<char> s;
QueType<char> q;
char ch;
char sItem, qItem;
int mismatches = 0;
cout << "Enter string: " << endl;
 
while(cin.peek() != '\\n') {
 
cin >> ch;
if(isalpha(ch)) {
 
if(!s.IsFull())
s.Push(toupper(ch));
 
if(!q.IsFull())
q.Enqueue(toupper(ch));
}
}

while( (!q.IsEmpty()) && (!s.IsEmpty()) ) {

s.Pop(sItem);
q.Dequeue(qItem);
 
if(sItem != qItem)
++mismatches;
}
if (mismatches == 0)
cout << "That is a palindrome" << endl;
else
cout << That is not a palindrome" << endl;
 
return 0;
}
Example: recognizing palindromes

Case Study: Simulation
•Queuing System: consists of servers and
queues of objects to be served.
•Simulation: a program that determines how
long items must wait in line before being
served.

Case Study: Simulation (cont.)
•Inputs to the simulation:
(1) the length of the simulation
(2) the average transaction time
(3) the number of servers
(4) the average time between job
arrivals

Case Study: Simulation (cont.)
•Parameters the simulation must vary:
(1) number of servers
(2) time between arrivals of items
 
•Output of simulation: average wait time.

Exercises (Chapt 4)
•26, 29-34, 39-41, 46, 47.
Tags