VKS-LEARNING HUB
QUEUE
VKS-LEARNING HUB
Queue is a linear data structure in which the insertion and deletion operations
are performed at two different ends.
0 1 2 3 4 5 6 7 8
R R R R R
F F F
• The insertion is performed at one end at a position which is known as 'rear'
• The deletion is performed at other end at a position which is known as 'front'.
• In queue data structure, the insertion and deletion operations are performed based
on FIFO (First In First Out) principle.
Queue
VKS-LEARNING HUB
Queue data structure in Python can be implemented
• Using Array( list )
When a queue is implemented using array, that queue
can organize only limited number of elements.
VKS-LEARNING HUB
Queue Using Array
• A queue data structure can be implemented using one dimensional
array. But, queue implemented using array can store only fixed
number of data values.
• The implementation of queue data structure using array is very
simple, just define a one dimensional array of int Or structure of
specific size and insert or delete the values into that array by
using FIFO (First In First Out) principle with the help of variable
'front' and 'rear'.
• Initially both ‘front’ and 'rear' are set to -1.
• Whenever, we want to insert a new value into the queue, increment
'rear' value by one and then insert at that position.
• Whenever we want to delete a value from the queue, then increment
'front' value by one and then display the value at 'front' position as
deleted element.
VKS-LEARNING HUB
Linear Array Queue Implementation
0 1 2 3 4 5
Stack[6]//size=6
Add(6);
front = -1
If rear=front=-1 then front=0;
rear=rear+1; stack[rear]=value
rear = -1
0
0
rear
front
Array Queue Example
0 1 2 3 4 5
Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
6
front = 0
rear = 0
1
2
3
4
rear= (rear=rear+1)//rear++
rear=(0+1)=1
insert item at this position
rear= (rear=rear+1)//rear++
rear=(1+1)=2
insert item at this position
rear= (rear=rear+1)//rear++
rear=(2+1)=3
insert item at this position
rear= (rear=rear+1)//rear++
rear=(3+1)=4
insert item at this position
rear
front
VKS-LEARNING HUB
0 1 2 3 4 5
Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
del();//front = 1
del();//front = 2
Add(9);
6
front = 0
4
make front = (0 + 1) = 1
1
7 3 8 9
rear 4
make front = (1 + 1) = 2
2
rear= (rear=rear+1)//rear++
rear=(4+1)=5
insert item at this position
5
Linear Array Queue Implementation
VKS-LEARNING HUB
0 1 2 3 4 5
Stack[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
del();//front = 1
del();//front = 2
Add(9);
Add(5);
front = 2
7 3 8 9
rear==Size-1 stack full!! No
more addition as per the
queue concept even
though there are two space
empty
rear = 4
5
Linear Array Queue Implementation
VKS-LEARNING HUB
Circular Arrays
• use a circular array to insert and remove items from a
queue The idea of a circular array is that the end of the
array “wraps around” to the start of the array
0
1
3
2
4
5
6
7
VKS-LEARNING HUB
0 1 2 3 4 5
Queue[6]//size=6
Add(6);
6
front = 0
If rear=front=-1 then front=0
Rear++
insert item at this position
Queue[Rear]=val
rear = -1
Circular Array Queue Implementation
0
VKS-LEARNING HUB
0 1 2 3 4 5
Queue[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
Add(10)
Add(15);
6
front = 0
4 7 3 8
rear = 0
1
2
3
4
rear=rear+1 //
rear=(0+1)=1
insert item at this position
rear=rear+1 //
rear=(1+1)=2
insert item at this position
rear=rear+1 //
rear=(2+1)=3
insert item at this position
rear=rear+1 //
rear=(3+1)=4
insert item at this position
Circular Array Queue Implementation
rear=rear+1 //
rear=(4+1)=5
insert item at this position
5
10
front=0 && rear=Size-1
front=0 && rear=5
Queue Overflow
VKS-LEARNING HUB
0 1 2 3 4 5
Queue[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
Add(10)
del();//front = 1
del();//front = 2
6
front = 0
4
Make front = front+1 //
front=0+1=1
1
7 3 8 10
rear = 5
2
Circular Array Queue Implementation
Make front = front+1 //
front=1+1=2
VKS-LEARNING HUB
Rear=size-1 && front!=0
rear=0
Insert element here
0 1 2 3 4 5
Queue[6]//size=6
Add(6)
Add(4)
Add(7)
Add(3)
Add(8)
Add(10);
del();//front = 1
del();//front = 2
Add(15);
Add(25);
Add(30);
front = 2
7 3 8 10
15
rear= 5
Circular Array Queue Implementation
0
1
25
front!=rear+1 // 2!=0+1
rear=rear+1 //0+1=1
Insert element here
front=rear+1
2=1+1
Queue Overflow
VKS-LEARNING HUB
‘Steps to create an empty queue.
Step 1: Include all the header files which are used in the program and define a
constant ‘MAX' with specific value.
Step 2: Declare a array with above defined MAX (queue=MAX*[None]) & two
variables 'front' and 'rear'
Step 3: Initialize both 'front' and 'rear' with '-1'. ( front = -1, rear = -1)
Step 4: Then implement main method by displaying menu of operations list and
make suitable function calls to perform operation selected by the user on queue.
MAX=5
queue=MAX*[None]
front=rear=-1
while True:
print('1. Insert')
print('2. Delete')
print('3. Display')
print('0. Exit')
ch=input('Choice[0-3]? ')
if ch=='1': qinsert(queue)
elif ch=='2': qdelete(queue)
elif ch=='3': qshow(queue)
elif ch=='0': break
VKS-LEARNING HUB
def qinsert(queue):
global front, rear
if front==0 and rear==MAX-1 or front==rear+1:
print('Queue Overflow')
else:
if front==-1:
front=rear=0
elif rear == MAX-1:
rear=0
else:
rear=rear+1
queue[rear]=float(input('Value? '))
Step 1: (If front=0 && rear=Max-1) or front=rear+1 Then Write ("Queue
Overflow")
else
Step 2: If front == -1 Then front=0 if rear== MAX-1 Then rear=0
Step 3: rear=rear+1//rear++
Step 4: queue_array[rear] = add_item ; count++
VKS-LEARNING HUB
def qdelete(queue):
global front, rear
if front==-1:
print('Queue Underflow')
else:
print(queue[front],'Deleted')
queue[front]=None
if front==-1:
front=0
elif front==rear:
front=rear=-1
else:
front+=1
Step 1: If front==-1 Then Write (“Queue Underflow") else
Step 2: display deleted item as Queue[front]
Step 3: if front==rear then front=rear=-1
Step 4 else if(front==size-1) then front=0 else front=front++
VKS-LEARNING HUB
display() - Displays the elements of a Queue
Step 1: Check whether queue is EMPTY. (front == rear)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the
function.
Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 4: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the
same until 'i' value is equal to rear (i <= rear)
def qshow(queue):
global front, rear
if front==-1:
print('Queue Empty')
else:
last=rear
if front>rear: last+=MAX
for k in range(front, last+1):
print(queue[k%MAX],end=' ')
print()

Understanding Queue in Python using list 2019.pptx

  • 1.
  • 2.
    VKS-LEARNING HUB Queue isa linear data structure in which the insertion and deletion operations are performed at two different ends. 0 1 2 3 4 5 6 7 8 R R R R R F F F • The insertion is performed at one end at a position which is known as 'rear' • The deletion is performed at other end at a position which is known as 'front'. • In queue data structure, the insertion and deletion operations are performed based on FIFO (First In First Out) principle. Queue
  • 3.
    VKS-LEARNING HUB Queue datastructure in Python can be implemented • Using Array( list ) When a queue is implemented using array, that queue can organize only limited number of elements.
  • 4.
    VKS-LEARNING HUB Queue UsingArray • A queue data structure can be implemented using one dimensional array. But, queue implemented using array can store only fixed number of data values. • The implementation of queue data structure using array is very simple, just define a one dimensional array of int Or structure of specific size and insert or delete the values into that array by using FIFO (First In First Out) principle with the help of variable 'front' and 'rear'. • Initially both ‘front’ and 'rear' are set to -1. • Whenever, we want to insert a new value into the queue, increment 'rear' value by one and then insert at that position. • Whenever we want to delete a value from the queue, then increment 'front' value by one and then display the value at 'front' position as deleted element.
  • 5.
    VKS-LEARNING HUB Linear ArrayQueue Implementation 0 1 2 3 4 5 Stack[6]//size=6 Add(6); front = -1 If rear=front=-1 then front=0; rear=rear+1; stack[rear]=value rear = -1 0 0 rear front
  • 6.
    Array Queue Example 01 2 3 4 5 Stack[6]//size=6 Add(6) Add(4) Add(7) Add(3) Add(8) 6 front = 0 rear = 0 1 2 3 4 rear= (rear=rear+1)//rear++ rear=(0+1)=1 insert item at this position rear= (rear=rear+1)//rear++ rear=(1+1)=2 insert item at this position rear= (rear=rear+1)//rear++ rear=(2+1)=3 insert item at this position rear= (rear=rear+1)//rear++ rear=(3+1)=4 insert item at this position rear front
  • 7.
    VKS-LEARNING HUB 0 12 3 4 5 Stack[6]//size=6 Add(6) Add(4) Add(7) Add(3) Add(8) del();//front = 1 del();//front = 2 Add(9); 6 front = 0 4 make front = (0 + 1) = 1 1 7 3 8 9 rear 4 make front = (1 + 1) = 2 2 rear= (rear=rear+1)//rear++ rear=(4+1)=5 insert item at this position 5 Linear Array Queue Implementation
  • 8.
    VKS-LEARNING HUB 0 12 3 4 5 Stack[6]//size=6 Add(6) Add(4) Add(7) Add(3) Add(8) del();//front = 1 del();//front = 2 Add(9); Add(5); front = 2 7 3 8 9 rear==Size-1 stack full!! No more addition as per the queue concept even though there are two space empty rear = 4 5 Linear Array Queue Implementation
  • 9.
    VKS-LEARNING HUB Circular Arrays •use a circular array to insert and remove items from a queue The idea of a circular array is that the end of the array “wraps around” to the start of the array 0 1 3 2 4 5 6 7
  • 10.
    VKS-LEARNING HUB 0 12 3 4 5 Queue[6]//size=6 Add(6); 6 front = 0 If rear=front=-1 then front=0 Rear++ insert item at this position Queue[Rear]=val rear = -1 Circular Array Queue Implementation 0
  • 11.
    VKS-LEARNING HUB 0 12 3 4 5 Queue[6]//size=6 Add(6) Add(4) Add(7) Add(3) Add(8) Add(10) Add(15); 6 front = 0 4 7 3 8 rear = 0 1 2 3 4 rear=rear+1 // rear=(0+1)=1 insert item at this position rear=rear+1 // rear=(1+1)=2 insert item at this position rear=rear+1 // rear=(2+1)=3 insert item at this position rear=rear+1 // rear=(3+1)=4 insert item at this position Circular Array Queue Implementation rear=rear+1 // rear=(4+1)=5 insert item at this position 5 10 front=0 && rear=Size-1 front=0 && rear=5 Queue Overflow
  • 12.
    VKS-LEARNING HUB 0 12 3 4 5 Queue[6]//size=6 Add(6) Add(4) Add(7) Add(3) Add(8) Add(10) del();//front = 1 del();//front = 2 6 front = 0 4 Make front = front+1 // front=0+1=1 1 7 3 8 10 rear = 5 2 Circular Array Queue Implementation Make front = front+1 // front=1+1=2
  • 13.
    VKS-LEARNING HUB Rear=size-1 &&front!=0 rear=0 Insert element here 0 1 2 3 4 5 Queue[6]//size=6 Add(6) Add(4) Add(7) Add(3) Add(8) Add(10); del();//front = 1 del();//front = 2 Add(15); Add(25); Add(30); front = 2 7 3 8 10 15 rear= 5 Circular Array Queue Implementation 0 1 25 front!=rear+1 // 2!=0+1 rear=rear+1 //0+1=1 Insert element here front=rear+1 2=1+1 Queue Overflow
  • 14.
    VKS-LEARNING HUB ‘Steps tocreate an empty queue. Step 1: Include all the header files which are used in the program and define a constant ‘MAX' with specific value. Step 2: Declare a array with above defined MAX (queue=MAX*[None]) & two variables 'front' and 'rear' Step 3: Initialize both 'front' and 'rear' with '-1'. ( front = -1, rear = -1) Step 4: Then implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on queue. MAX=5 queue=MAX*[None] front=rear=-1 while True: print('1. Insert') print('2. Delete') print('3. Display') print('0. Exit') ch=input('Choice[0-3]? ') if ch=='1': qinsert(queue) elif ch=='2': qdelete(queue) elif ch=='3': qshow(queue) elif ch=='0': break
  • 15.
    VKS-LEARNING HUB def qinsert(queue): globalfront, rear if front==0 and rear==MAX-1 or front==rear+1: print('Queue Overflow') else: if front==-1: front=rear=0 elif rear == MAX-1: rear=0 else: rear=rear+1 queue[rear]=float(input('Value? ')) Step 1: (If front=0 && rear=Max-1) or front=rear+1 Then Write ("Queue Overflow") else Step 2: If front == -1 Then front=0 if rear== MAX-1 Then rear=0 Step 3: rear=rear+1//rear++ Step 4: queue_array[rear] = add_item ; count++
  • 16.
    VKS-LEARNING HUB def qdelete(queue): globalfront, rear if front==-1: print('Queue Underflow') else: print(queue[front],'Deleted') queue[front]=None if front==-1: front=0 elif front==rear: front=rear=-1 else: front+=1 Step 1: If front==-1 Then Write (“Queue Underflow") else Step 2: display deleted item as Queue[front] Step 3: if front==rear then front=rear=-1 Step 4 else if(front==size-1) then front=0 else front=front++
  • 17.
    VKS-LEARNING HUB display() -Displays the elements of a Queue Step 1: Check whether queue is EMPTY. (front == rear) Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function. Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'. Step 4: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value is equal to rear (i <= rear) def qshow(queue): global front, rear if front==-1: print('Queue Empty') else: last=rear if front>rear: last+=MAX for k in range(front, last+1): print(queue[k%MAX],end=' ') print()