.. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check...
-
Upload
addison-groves -
Category
Documents
-
view
226 -
download
1
Transcript of .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check...
![Page 1: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/1.jpg)
.
![Page 2: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/2.jpg)
2
A QUEUE IS A CONTAINER IN WHICH:
. INSERTIONS ARE MADE ONLY AT THE BACK;
. DELETIONS, RETRIEVALS, ANDMODIFICATIONS ARE MADE ONLYAT THE FRONT.
![Page 3: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/3.jpg)
3
PUSH (ALSO CALLED ENQUEUE) -- TOINSERT AN ITEM AT THE BACK
POP (ALSO CALLED DEQUEUE) -- TODELETE THE FRONT ITEM
IN A QUEUE, THE FIRST ITEM
INSERTED WILL BE THE FIRST ITEM
DELETED: FIFO (FIRST-IN, FIRST-OUT)
![Page 4: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/4.jpg)
A queue is a waiting line……. It’s in daily life:-
A line of persons waiting to check out at a supermarket.
A line of persons waiting to purchase a ticket for a film.
A line of planes waiting to take off at an airport.
A line of vehicles at a toll booth.
![Page 5: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/5.jpg)
Difference between Stack and Queues:
Stack is last-in-first-out (LIFO)
Queue is first-in-first-out (FIFO)
![Page 6: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/6.jpg)
Unlike stacks in which elements are popped and pushed only at the ends of the list,
Collection of data elements: items are removed from a queue at one end,
called the FRONT of the queue; and elements are added at the other end,
called the BACK
![Page 7: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/7.jpg)
Basic operations Construct a queue Check if empty Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front)
![Page 8: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/8.jpg)
Consider an array in which to store a queue
Note additional variables needed myFront, myBack
Picture a queueobject like this
![Page 9: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/9.jpg)
Empty Queue
Enqueue(70)
![Page 10: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/10.jpg)
Enqueue(80)
Enqueue(50)
![Page 11: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/11.jpg)
Dequeue()
Dequeue()
![Page 12: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/12.jpg)
Enqueue(90)
Enqueue(60)
![Page 13: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/13.jpg)
Problems We quickly "walk off the end" of the array
Possible solutions Shift array elements Use a circular queue
Note that both emptyand full queuegives myBack == myFront
![Page 14: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/14.jpg)
Use Linear Array to implement a queue.
Waste of memory: The deleted elements can not be re-used. Solution: to use circular queue. Two implementations:
Using n-1 space. Using n space + full tag
![Page 15: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/15.jpg)
Create(Q)
Q: Array[0…n-1]
front = rear = 0 //initialize Enqueue(item, Q) Queue
begin
rear = (rear+1) mod nrear = (rear+1) mod n; //rear moves forward;
if rear = front
QueueFull; // Queue is full.
rear = rear-1 mod n; // rear back to the previous position;
else
Q[rear]=item;
end;
0 1 2 … n-1
R R = (R+1) mod n
![Page 16: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/16.jpg)
Dequeue(Q) item
begin
if front=rear front=rear
QueueEmpty;
else
front = (front+1) mod n;
item = Q[front];
end;
end; Note: only ((n-1 n-1 ) ) space used;
FFRR
X
X
XX X
X
X
X
X
XXX
![Page 17: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/17.jpg)
If an item were stored in the last position, and an Enqueure() occurred.
myBack would be incremented by 1, giving it the same value as myFront .
However, myFront == myBack indicates the queue is empty.
Thus, we cannot distinguish between empty and full.
We may avoid this situation by maintaining one empty position, so that myFront will never equal to myBack unless the queue is empty.
![Page 18: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/18.jpg)
Construct:Create an array, set capacity, myFront=myBack=0
Empty:test myFront==myBack
Front :if not empty:
print array[myFront]
![Page 19: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/19.jpg)
A parameter “Tag” is introduced to help to make sure the queue is Empty or Full:
Boolean
If Tag = True, combined with other conditions => queue is Full
If Tag = False, combined with other conditions => queue is Null
““TagTag”” ccan determine the states of the queue solely!an determine the states of the queue solely!
![Page 20: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/20.jpg)
1. Set newBack == (myBack+1)%Queue_capacity
2. If newBack == myFrontSignal “Queue Full”
otherwise:Set Array[myBack] == valueSet myBack == newBack
![Page 21: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/21.jpg)
If queue is emptysignal “Queue Empty”
OtherwiseSet myFront=(myFront+1)%Queue_capacity
![Page 22: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/22.jpg)
Circular Linklist
![Page 23: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/23.jpg)
A Circular Linked List is a special type of Linked List
It supports traversing from the end of the list to the beginning by making the last node point back to the head of the list
A Rear pointer is often used instead of a Head pointer
Rear
10 20 40 7055
![Page 24: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/24.jpg)
Circular linked lists are usually sorted Circular linked lists are useful for
playing video and sound files in “looping” mode
They are also a stepping stone to implementing graphs, an important topic in comp171
![Page 25: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/25.jpg)
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
typedef Node* NodePtr;
![Page 26: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/26.jpg)
Nodes contain references to other nodes Example
Issues Easy to find succeeding elements Start from head of list for preceding elements
![Page 27: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/27.jpg)
Reference-based linked list Insertion / deletion = O(1) Indexing = O(n) Easy to dynamically increase size of list
Array Insertion / deletion = O(n) Indexing = O(1) Compact, uses less space Easy to copy, merge Better cache locality
![Page 28: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/28.jpg)
1. Original list & new element temp
2. Modify temp.next cursor.next
![Page 29: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/29.jpg)
3. Modify cursor.next temp
4. Modify cursor temp
![Page 30: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/30.jpg)
1. Find before such that before.next = cursor
2. Modify before.next cursor.next
![Page 31: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/31.jpg)
3. Delete cursor
4. Modify cursor before.next
![Page 32: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/32.jpg)
insertNode(NodePtr& Rear, int item)//add new node to ordered circular linked list
deleteNode(NodePtr& Rear, int item)//remove a node from circular linked list
print(NodePtr Rear)//print the Circular Linked List once
![Page 33: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/33.jpg)
void print(NodePtr Rear){
NodePtr Cur;
if(Rear != NULL){
Cur = Rear->next;
do{
cout << Cur->data << " ";
Cur = Cur->next;
}while(Cur != Rear->next);
cout << endl;
}
}
Rear
10 20 40 7055
![Page 34: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/34.jpg)
Insert into an empty list
Rear
10
New
NotePtr New = new Node;New->data = 10;
Rear = New;Rear->next = Rear;
![Page 35: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/35.jpg)
Insert to head of a Circular Linked List
Rear
New
New->next = Cur; // same as: New->next = Rear->next;
Prev->next = New; // same as: Rear->next = New;
PrevCur
10 20 40 55 70
![Page 36: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/36.jpg)
Insert to middle of a Circular Linked List between Pre and Cur
Prev
New
New->next = Cur;
Prev->next = New;
RearCur
10 5520
40
70
![Page 37: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/37.jpg)
Insert to end of a Circular Linked List
RearNew
New->next = Cur; // same as: New->next = Rear->next;
Prev->next = New; // same as: Rear->next = New;
Rear = New;
PrevCur
10 20 40 55 70
![Page 38: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/38.jpg)
Delete a node from a single-node Circular Linked List
Rear = Cur = Prev
Rear = NULL;
delete Cur;
10
![Page 39: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/39.jpg)
Delete the head node from a Circular Linked List
RearCur
Prev->next = Cur->next;// same as: Rear->next = Cur->next
delete Cur;
Prev
10 20 40 55 70
![Page 40: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/40.jpg)
Delete a middle node Cur from a Circular Linked List
Prev RearCur
Prev->next = Cur->next;
delete Cur;
10 20 40 55 70
![Page 41: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/41.jpg)
Delete the end node from a Circular Linked List
Rear
Prev->next = Cur->next; // same as: Rear->next;
delete Cur;
Rear = Prev;
Prev Cur
10 20 40 55 70
![Page 42: .. 2 3 A queue is a waiting line……. It’s in daily life:- A line of persons waiting to check out at a supermarket. A line of persons waiting.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649cb85503460f9497e745/html5/thumbnails/42.jpg)
.