Queues in C++ detailed explanation and examples .ppt
Jamiluddin39
18 views
52 slides
Dec 19, 2024
Slide 1 of 52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
About This Presentation
Queues in C++ with explanation
Size: 863.61 KB
Language: en
Added: Dec 19, 2024
Slides: 52 pages
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
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
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
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).
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.
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