Download presentation
Presentation is loading. Please wait.
Published byAddison Groves Modified over 9 years ago
1
.
2
2
3
3
4
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.
5
Difference between Stack and Queues: Stack is last-in-first-out (LIFO) Queue is first-in-first-out (FIFO)
6
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
7
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)
8
Consider an array in which to store a queue Note additional variables needed myFront, myBack Picture a queue object like this
9
Empty Queue Enqueue(70)
10
Enqueue(80) Enqueue(50)
11
Dequeue()
12
Enqueue(90) Enqueue(60)
13
Problems We quickly "walk off the end" of the array Possible solutions Shift array elements Use a circular queue Note that both empty and full queue gives myBack == myFront
14
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
15
Create(Q) Q: Array[0…n-1] front = rear = 0 //initialize Enqueue(item, Q) Queue begin rear = (rear+1) mod n rear = (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; 012… n-1 R R = (R+1) mod n
16
Dequeue(Q) item begin front=rear if front=rear QueueEmpty; else front = (front+1) mod n; item = Q[front]; end; (n-1 ) Note: only (n-1 ) space used; F R X X X XX X X X X XX X
17
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.
18
Construct: Create an array, set capacity, myFront=myBack=0 Empty: test myFront==myBack Front : if not empty: print array[myFront]
19
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 “Tag” can determine the states of the queue solely!
20
1. Set newBack == (myBack+1)%Queue_capacity 2. If newBack == myFront Signal “Queue Full” otherwise: Set Array[myBack] == value Set myBack == newBack
21
If queue is empty signal “Queue Empty” Otherwise Set myFront=(myFront+1)%Queue_capacity
22
Circular Linklist
23
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 1020407055
24
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
25
#include using namespace std; struct Node{ int data; Node* next; }; typedef Node* NodePtr;
26
Nodes contain references to other nodes Example Issues Easy to find succeeding elements Start from head of list for preceding elements
27
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
28
1. Original list & new element temp 2. Modify temp.next cursor.next
29
3. Modify cursor.next temp 4. Modify cursor temp
30
1. Find before such that before.next = cursor 2. Modify before.next cursor.next
31
3. Delete cursor 4. Modify cursor before.next
32
* 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
33
void print(NodePtr Rear){ NodePtr Cur; if(Rear != NULL){ Cur = Rear->next; do{ cout data << " "; Cur = Cur->next; }while(Cur != Rear->next); cout << endl; } Rear 1020407055
34
Insert into an empty list Rear 10 New NotePtr New = new Node; New->data = 10; Rear = New; Rear->next = Rear;
35
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 1020405570
36
Insert to middle of a Circular Linked List between Pre and Cur Prev New New->next = Cur; Prev->next = New; RearCur 1055204070
37
Insert to end of a Circular Linked List Rear New New->next = Cur;// same as: New->next = Rear->next; Prev->next = New;// same as: Rear->next = New; Rear = New; Prev Cur 1020405570
38
Delete a node from a single-node Circular Linked List Rear = Cur = Prev Rear = NULL; delete Cur; 10
39
Delete the head node from a Circular Linked List Rear Cur Prev->next = Cur->next;// same as: Rear->next = Cur->next delete Cur; Prev 1020405570
40
Delete a middle node Cur from a Circular Linked List Prev Rear Cur Prev->next = Cur->next; delete Cur; 1020405570
41
Delete the end node from a Circular Linked List Rear Prev->next = Cur->next; // same as: Rear->next; delete Cur; Rear = Prev; Prev Cur 1020405570
42
.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.