0% found this document useful (0 votes)
14 views26 pages

Lecture 4

Uploaded by

umtxengineer3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Lecture 4

Uploaded by

umtxengineer3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture No.

Data Structures and


Algorithms
Queues

 A queue is a FIFO (First-In First-Out ) structure.


 A queue is a linear structure for which items can be
only inserted at one end and removed at another end.
Queue Operations

Enqueue(X) – place X at the rear of the


queue.
Dequeue() -- remove the front element and
return it.
Front() -- return front element without removing
it.
IsEmpty() -- return TRUE if queue is
empty, FALSE otherwise
Implementing Queue(through SLL)

 Using linked List: Recall


 Insert works in constant time for either end of a
linked list.
 Remove works in constant time only.
 Seems best that head of the linked list be the front of
the queue so that all removes will be from the front.
 Inserts will be at the end of the list.
Implementing Queue(through
SLL)
• #include<iostream>
• #include<cstdlib>
• using namespace std;
• struct node{
• int info;
• struct node *next;
• };
• class Queue{
• private:
• node *rear;
• node *front;
• public:

• Queue(){
• rear = NULL;
• front = NULL;
• }
Implementing Queue(through
SLL)
enqueue()
{
• int data;
• node *temp = new node;
• cout<<"Enter the data to enqueue: ";
• cin>>data;
• temp->info = data;
• temp->next = NULL;
• if(front == NULL){
• front = temp;
• rear=temp;
• }else{
• rear->next = temp;
• }
• rear = temp;
•}
Implementing Queue(through
SLL)
• dequeue(){
• node *temp = new node;
• if(front == NULL){
• cout<<"\nQueue is Emtpty\n";
• }else{
• temp = front;
• front = front->next;
• cout<<"The data Dequeued is "<<temp->info;
• delete temp;
• }
}
Implementing Queue

 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 1 7 5 2
Implementing Queue

 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

enqueue(9)

front rear front rear

7 5 2 9 7 5 2 9
Implementing Queue

• display(){
• node *p = new node;
• p = front;
• if(front == NULL){
• cout<<"\nNothing to Display\n";
• }else{
• while(p!=NULL){
• cout<< p->info <<endl;
• p = p->next;
• }
• }
•}
• };
Implementing Queue

• int main(){
• Queue queue;
• int choice;
• while(true){
• cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";
• cout<<"\nEnter your choice: ";
• cin>>choice;
• switch(choice){
• case 1:
• queue.enqueue();
• break;

Implementing Queue

• case 2:
• queue.dequeue();
• break;
• case 3:
• queue.display();
• break;
• case 4:
• exit(0);
• break;
• default:
• cout<<"\nInvalid Input. Try again! \n";
• break;
• }}
• return 0;}
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.
Queue using Array

enqueue(6)

front rear
1 7 5 2 6
1 7 5 2 6
0 1 2 3 4 5 6 7
front rear
0 4
Queue using Array

enqueue(8)

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
0 5
Queue using Array

dequeue()

front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
1 5
Queue using Array

dequeue()

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
front rear
2 5
Queue using Array

enqueue(9)
enqueue(12)
front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
front rear
2 7
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.

0 1
front
front rear
7 2 2
12 5
5 2 6 8 9 12
6 9 2
8 6 3 rear
7
5 4
Queue using Array

enqueue(21)
0 1
front rear 21 front size
7 2 2 8
12 5
5 2 6 8 9 12 21
6 9 2
8 6 3 rear noElemen
0 7 ts
5 4
Queue using Array

enqueue(7)
0 1
front rear 21 7 front size
7 2 2 8
12 5
5 2 6 8 9 12 21 7
6 9 2
8 6 3 rear noElemen
1 8 ts
5 4
Queue using Array

dequeue()
0 1

front rear 21 7 front size


7 2 4 8
12
6 8 9 12 21 7
6 9
8 6 3 rear noElemen
1 6 ts
5 4
Use of Queues

 Out of the numerous uses of the queues, one of the


most useful is simulation.
 A simulation program attempts to model a real-world
phenomenon.
 Many popular video games are simulations, e.g.,
SimCity, FlightSimulator
 Each object and action in the simulation has a
counterpart in real world.
Uses of Queues

 If the simulation is accurate, the result of the


program should mirror the results of the real-world
event.
 Thus it is possible to understand what occurs in the
real-world without actually observing its occurrence.
 Let us look at an example. Suppose there is a bank
with four tellers.
Simulation of a Bank

 A customer enters the bank at a specific time (t1)


desiring to conduct a transaction.
 Any one of the four tellers can attend to the
customer.
 The transaction (withdraw, deposit) will take a
certain period of time (t2).
 If a teller is free, the teller can process the
customer’s transaction immediately and the
customer leaves the bank at t1+t2.

You might also like