Lecture three of datat structures ,.The Queue-ds.ppt
1. 3.1 Algorithms for Queue Operations
3.2 Other Queues
3.3 Circular Queue
3.4 Deques
3.5 Applications of Queue
3. The Queue
1
2. A queue is logically a first in first out (FIFO or first come
first serve) linear data structure.
The concept of queue can be understood by our real
life problems. For example a customer come and join in
a queue to take the train ticket at the end (rear) and the
ticket is issued from the front end of queue. That is, the
customer who arrived first will receive the ticket first. It
means the customers are serviced in the order in which
they arrive at the service centre.
It is a homogeneous collection of elements in which
new elements are added at one end called rear, and
the existing elements are deleted from other end called
front.
Definition
2
3. The basic operations that can be performed on queue:
1. Insert (or add) an element to the queue (push)
2. Delete (or remove) an element from a queue (pop)
Operations
3
6. Queue can be implemented in two ways:
1. Using arrays (static)
2. Using pointers (dynamic)
underflow and overflow conditions when a queue is
implemented using arrays.
If we try to pop (or delete or remove) an element
from queue when it is empty, underflow occurs.
If we try to push (or insert or add) an element to
queue when queue is full, overflow occurs.
Implementation
6
7. Inserting an element into the queue
1. Initialize front=-1, rear = –1
2. Input the value to be inserted and assign to
variable “data”
3. If (rear == SIZE-1)
(a) Display “Queue overflow”
(b) Exit
4. Else
(a) If (front==-1) front=0;
(b) rear = rear +1
5. Q[rear] = data
6. Exit
3.1 Algorithms for Queue Operations
7
8. Deleting an element from queue
1. If (rear== front==-1)
(a) Display “the queue is empty”
(b) Exit
2. Else
(a) data = Q[front]
3. front = front +1
4. Exit
Algorithms for Queue Operations
8
9. // Program to implement queue using arrays
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#define MAX 50
int queue_arr[MAX];
int rear=-1;
int front=-1;
Queue Operations-C Program(1)
9
10. // This function will insert an element to the queue
void insert()
{ int added_item;
if(rear==MAX-1)
{ printf("nQueue Overflown");
getch();
return;
}
else
{ if(front==-1) front=0; // If queue is initially empty
printf("nInput the element for adding in queue:");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear]=added_item; // Inserting the element
}
} // End of insert()
Queue Operations-C Program(2)
10
11. // This function will delete (or pop) an element from the queue
void del()
{ if(front==-1 || front>rear)
{
printf("nQueue Underflown");
return;
}
else
{
//deleteing the element
printf("nElement deleted from queue is :%d", queue_arr[front]);
front=front+1;
}
} // End of del()
Queue Operations-C Program(3)
11
12. // Displaying all the elements of the queue
void display()
{ int i;
// Checking whether the queue is empty or not
if (front==-1 || front>rear)
{ printf("nQueue is emptyn");
return;
}
else
{ printf("nQueue is:n");
for(i=front; i<=rear; i++)
printf("%d ",queue_arr[i]);
printf("n");
}
} // End of display()
Queue Operations-C Program(4)
12
13. int main()
{ int choice;
while(1)
{ system("cls"); // Menu options
printf("n1. Insertn");
printf("2. Deleten");
printf("3. Displayn");
printf("4. Quitn");
printf("nEnter your choice:");
scanf("%d", &choice);
Queue Operations-C Program(5)
13
14. switch(choice)
{ case 1: insert();
break;
case 2: del();
getch();
break;
case 3: display();
getch();
break;
case 4: exit(1);
default: printf("nWrong choice.n");
getch();
} // End of switch
} // End of while
} // End of main()
Queue Operations-C Program(6)
14
15. There are three major variations in a simple queue
1. Circular queue
2. Double ended queue (de-queue)
3. Priority queue
Priority queue is generally implemented using linked
list, which is discussed in the chapter 5. The other
two queue variations are discussed in the following
sections.
3.2 Other Queues
15
16. In circular queues the elements Q[0],Q[1],Q[2] …
Q[n–1] is represented in a circular fashion with
Q[0] following Q[n-1]. A circular queue is one in
which the insertion of a new element is done at
the very first location of the queue if the last
location at the queue is full.
3.3 Circular Queue
16
21. Let Q be the array of some specified size say SIZE.
FRONT and REAR are two pointers where the
elements are deleted and inserted at two ends of the
circular queue. DATA is the element to be inserted.
Algorithm
21
22. Inserting an element to circular Queue
1. Initialize FRONT = –1; REAR = –1
2. REAR = (REAR + 1) % SIZE
3. If (FRONT is equal to REAR)
(a) Display “Queue is full”
(b) Exit
4. Else
(a) Input the value to be inserted and assign to variable
“DATA”
5. If (FRONT is equal to –1)
(a) FRONT = 0
(b) REAR = 0
6. Q[REAR] = DATA
7. Repeat steps 2 to 6 if we want to insert more elements
8. Exit
Algorithm-Enqueue
22
23. Deleting an element from a circular queue
1. If (FRONT is equal to –1)
(a) Display “Queue is empty”
(b) Exit
2. Else
(a) DATA = Q[FRONT]
3. If (REAR is equal to FRONT)
(a) FRONT = –1
(b) REAR = –1
4. Else
(a) FRONT = (FRONT +1) % SIZE
5. Repeat the steps 1 to 4 if we want to delete more
elements
6. Exit
Algorithm-Dequeue
23
24. A deque is a homogeneous list in which elements can
be added or inserted (called push operation) and
deleted or removed (called pop operation) from both
the ends. i.e., we can add a new element at the rear or
front end and also we can remove an element from
both front and rear end. Hence it is called Double
Ended Queue.
There are two types of deque depending upon the
restriction to perform insertion or deletion operations at
the two ends.
1. Input restricted deque
2. Output restricted deque
3.4 Deques
24
25. An input restricted deque is a deque, which allows insertion
at only one end, rear end, but allows deletion at both ends,
rear and front end of the lists.
An output-restricted deque is a deque, which allows deletion
at only one end, front end, but allows insertion at both ends,
rear and front ends of the lists.
The possible operation performed on deque is
1. Add an element at the rear end
2. Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Only 1st, 3rd and 4th operations are performed by input-
restricted deque and 1st, 2nd and 3rd operations are
performed by output-restricted deque.
Operations
25
26. Let Q be the array of MAX elements. front (or left)
and rear (or right) are two array index (pointers),
where the addition and deletion of elements
occurred.
Let DATA be the element to be inserted. Before
inserting any element to the queue left and right
pointer will point to the –1.
DATA will contain the element just deleted.
Algorithm
26
27. Insert an element at the right side of the deque
1. Input the DATA to be inserted
2. If ((left == 0 && right == MAX–1) || (left == right + 1))
(a) Display “Queue Overflow”
(b) Exit
3. If (left == –1)
(a) left = 0
(b) right = 0
4. Else
(a) if (right == MAX–1)
(i) right = 0
(b) else
(i) right = right+1
5. Q[right] = DATA
6. Exit
Algorithm-enqueue
27
28. Insert an element at the left side of the deque
1. Input the DATA to be inserted
2. If ((left == 0 && right == MAX–1) || (left == right+1))
(a) Display “Queue Overflow”
(b) Exit
3. If (left == –1)
(a) Left = 0
(b) Right = 0
4. Else
(a) if (left == 0)
(i) left = MAX–1
(b) else
(i) left = left–1
5. Q[left] = DATA
6. Exit
Algorithm-Enqueue
28
29. Delete an element from the right side of the deque
1. If (left == –1)
(a) Display “Queue Underflow”
(b) Exit
2. DATA = Q [right]
3. If (left == right)
(a) left = –1
(b) right = –1
4. Else
(a) if(right == 0)
(i) right = MAX–1
(b) else
(i) right = right–1
5. Exit
Algorithm-Dequeue
29
30. Delete an element from the left side of the de-queue
1. If (left == –1)
(a) Display “Queue Underflow”
(b) Exit
2. DATA = Q [left]
3. If(left == right)
(a) left = –1
(b) right = –1
4. Else
(a) if (left == MAX–1)
(i) left = 0
(b) Else
(i) left = left +1
5. Exit
Algorithm-Dequeue
30
31. // Program to implement input and output
// Restricted deque using arrays
#include<conio.h>
#include<stdio.h>
#include<process.h>
#define MAX 50
int deque_arr[MAX];
int left=-1;
int right=-1;
Deque-C Program(1)
31
32. // This function will insert an element at the right side of the deque
void insert_right()
{ int added_item;
if((left==0 && right==MAX-1) || (left==right+1))
{ printf("nQueue Overflown");
getch();
return; }
if(left==-1) // if queue is initially empty
{ left=0; right=0; }
else
if(right==MAX-1) // right is at last position of queue
right=0;
else right=right+1;
printf("n Input the element for adding in queue: ");
scanf ("%d", &added_item); // Inputting the element at the right
deque_arr[right]=added_item;
} // End of insert_right()
Deque-C Program(2)
32
33. // Function to insert an element at the left position of the deque
void insert_left()
{ int added_item;
// Checking for queue overflow
if((left==0 && right==MAX-1) || (left==right+1))
{ printf("nQueue Overflow.n");
getch();
return; }
if(left==-1) // If queue is initially empty
{ left=0; right=0; }
else
if(left==0) left=MAX-1;
else left = left-1;
printf("nInput the element for adding in queue:");
scanf ("%d", &added_item); // inputting at the left side of the queue
deque_arr[left]=added_item;
} // End of insert_left()
Deque-C Program(3)
33
34. // This function will delete an element from the left side of the deque
void delete_left()
{ // Checking for queue underflow
if(left==-1)
{ printf("nQueue Underflown");
return; }
// deleting the element from the left side
printf("nElement deleted from queue is: %dn", deque_arr[left]);
if(left==right) // Queue has only one element
{ left=-1; right=-1;
}
else
if(left==MAX-1) left=0;
else left=left+1;
} // End of delete_left()
Deque-C Program(4)
34
35. // Function to delete an element from the right hand side of the deque
void delete_right()
{ // Checking for underflow conditions
if(left==-1)
{ printf("nQueue Underflown");
return;
}
printf("nElement deleted from queue is: %dn", deque_arr[right]);
if(left==right) // queue has only one element
{ left=-1; right=-1;
}
else
if(right==0) right=MAX-1;
else right=right-1;
} // End of delete_right()
Deque-C Program(5)
35
36. // Displaying all the contents of the queue
void display_queue()
{ int front_pos=left, rear_pos=right;
// Checking whether the queue is empty or not
if(left==-1)
{ printf("nQueue is empty.n");
return; }
// displaying the queue elements
printf("nQueue elements:n");
if(front_pos<=rear_pos)
{ while(front_pos<=rear_pos)
{ printf("%d ", deque_arr[front_pos]);
front_pos++;
}
}
Deque-C Program(6)
36
37. else
{ while(front_pos<=MAX-1)
{
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
front_pos=0;
while(front_pos<=rear_pos)
{
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
} // End of else
printf("n");
} // End of display_queue()
Deque-C Program(7)
37
38. // Function to implement all the operation of the input restricted deque
void input_que()
{ int choice;
while(1)
{
system("cls");
//menu options to input restricted deque
printf("n1. Insert at rightn");
printf("2. Delete from leftn");
printf("3. Delete from rightn");
printf("4. Displayn");
printf("5. Quitn");
printf("nEnter your choice: ");
scanf("%d", &choice);
Deque-C Program(8)
38
39. switch(choice)
{ case 1: insert_right();
break;
case 2: delete_left();
getch();
break;
case 3: delete_right();
getch();
break;
case 4: display_queue();
getch();
break;
case 5: exit(0);
default: printf("nWrong choicen");
getch();
} // End of switch
} // End of while
} // End of input_que()
Deque-C Program(9)
39
40. // This function will implement all the operation of the output restricted deque
void output_que()
{
int choice;
while(1)
{
system("cls");
//menu options for output restricted deque
printf("n1. Insert at rightn");
printf("2. Insert at leftn");
printf("3. Delete from leftn");
printf("4. Displayn");
printf("5. Quitn");
printf("nEnter your choice:");
scanf("%d", &choice);
Deque-C Program(10)
40
41. switch(choice)
{ case 1: insert_right();
break;
case 2: insert_left();
break;
case 3: delete_left();
getch();
break;
case 4: display_queue();
getch();
break;
case 5: exit(0);
default: printf("nWrong choicen");
getch();
} // End of switch
} // End of while
} // End of output_que()
Deque-C Program(11)
41
42. int main()
{ int choice;
system("cls");
// Main menu options
printf("n1. Input restricted dequen");
printf("2. Output restricted dequen");
printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
{ case 1: input_que();
break;
case 2: output_que();
break;
default: printf("nWrong choicen");
} // End of switch
} // End of main()
Deque-C Program(12)
42
43. round robin techniques for processor scheduling
is implemented using queue.
printer server routines (in drivers) are designed
using queues.
all types of customer service software (like
railway/air ticket reservation) are designed using
queue to give proper service to the customers.
3.5 Applications of Queue
43