# Chapter 4: The Queue¶

## Example 1, page no. 68¶

In [ ]:
import sys

MAX = 50
queue_arr = []
rear = [-1]
front = [-1]

def insert():
if (rear[0] == MAX-1):
print "Queue is full..."
return
else:
if(front[0] == -1):
front[0] = 0
print "Input the element to be added: "
rear[0] = rear[0]+1

def que_del(): # del() is an inbuilt function in python so we name it as que_del here
print front[0]
if(front[0] == -1 or front[0]>rear[0]):
print "Queue is empty..."
return
else:
dell = front[0]
print "Deleted element from queue is: ", queue_arr[dell]
front[0] = front[0]+1

def display():
if(front[0] == -1 or front[0]>rear[0]):
print "Queue is empty..."
return
else:
print "Queue is: "
for i in range(front[0],rear[0]+1):
print queue_arr[i],

while(1):
print "\n1. Insert"
print "2. Delete"
print "3. Display"
print "4. Quit"
choice = raw_input()
if choice == '1':
insert()
elif choice == '2':
que_del()
elif choice == '3':
display()
elif choice == '4':
sys.exit()
else:
print "You entered an invalid choice..."

1. Insert
2. Delete
3. Display
4. Quit


## Example 2, page no. 73¶

In [3]:
import sys
MAX = 50

class circular_queue:
def __init__(self):
self.cqueue_arr = []
self.rear = -1
self.front = -1

def insert(self):
if((self.front == 0 and self.rear == MAX-1) or (self.front == self.rear+1)):
print "Queue is full..."
return
if(self.front == -1):
self.front = 0
self.rear = 0
else:
if(self.rear == MAX-1):
rear = 0
else:
self.rear = self.rear+1
print "Enter element to be added: "

def que_del(self):
if(self.front == -1):
print "Queue is empty..."
return
print "Element deleted from queue is: ", self.cqueue_arr[self.front]
if(self.front == self.rear):
self.front = -1
self.rear = -1
else:
if(self.front == MAX-1):
self.front = 0
else:
self.front += 1

def display(self):
front_pos = self.front
rear_pos = self.rear
if(self.front == -1):
print "Queue is empty..."
return
print "Queue Elements: ",
if(front_pos <= rear_pos):
while(front_pos <= rear_pos):
print " ", self.cqueue_arr[front_pos],
front_pos += 1

co = circular_queue()
while(1):
print "\n1. Insert"
print "2. Delete"
print "3. Display"
print "4. Quit"
choice = int(raw_input())
if choice == 1:
co.insert()
elif choice == 2:
co.que_del()
elif choice == 3:
co.display()
elif choice == 4:
sys.exit()
else:
print "You entered an invalid choice..."

1. Insert
2. Delete
3. Display
4. Quit
1
5

1. Insert
2. Delete
3. Display
4. Quit
1
10

1. Insert
2. Delete
3. Display
4. Quit
1
15

1. Insert
2. Delete
3. Display
4. Quit
3
Queue Elements:    5   10   15
1. Insert
2. Delete
3. Display
4. Quit
4

An exception has occurred, use %tb to see the full traceback.

SystemExit

To exit: use 'exit', 'quit', or Ctrl-D.


## Example 3, page no. 80¶

In [5]:
import sys

MAX = 50
left = -1
right = -1
dqueue_arr = []

# insert element to right
def insert_right():
global left, right, dqueue_arr
if ((left == 0 and right == MAX-1) or (left == right+1)):
print "Queue is full..."
return
if(left == -1):
left = 0
right = 0
else:
if(right == MAX-1):
right = 0;
else:
right = right+1;
print "Input the element for adding in queue: "
# Inputting the element at the right

def insert_left():
global left, right, dqueue_arr
# Checking for queue overflow
if ((left == 0 and right == MAX-1) or (left == right+1)):
print "Queue is full..."
return
if (left == -1): # If queue is initially empty
left = 0;
right = 0;
else:
if (left== 0):
left = MAX -1;
else:
left = left-1;
print "\nInput the element for adding in queue:"
# inputting at the left side of the queue

# This function will delete an element from the queue from the left side
def delete_left():
global left, right, dqueue_arr
# Checking for queue underflow
if (left == -1):
print "Queue is empty... "
return
# deleting the element from the left side
print "Element deleted from queue is: ", dqueue_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

# Function to delete an element from the right hand side of the de-queue
def delete_right():
global left, right, dqueue_arr
# Checking for underflow conditions
if (left == -1):
print "Queue is empty..."
return
print "Element deleted from queue is : ", dqueue_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

# Displaying all the contents of the queue
def display_queue():
global left, right, dqueue_arr
front_pos = left
rear_pos = right
# Checking whether the queue is empty or not
if (left == -1):
print "Queue is empty..."
return
# displaying the queue elements
print "Queue elements :",
if (front_pos <= rear_pos):
while(front_pos <= rear_pos):
print " ", dqueue_arr[front_pos],
front_pos = front_pos + 1

# Function to implement all the operation of the input restricted queue
def input_que():
global left, right, dqueue_arr
while(1):
# menu options to input restricted queue
print "\n1.Insert at right"
print "2.Delete from left\n"
print "3.Delete from right\n"
print "4.Display\n"
print "5.Quit\n"
print "\nEnter your choice : "
choice = int(raw_input())
if choice == 1:
insert_right()
display_queue()
elif choice == 2:
delete_left()
elif choice == 3:
delete_right()
elif choice == 4:
display_queue()
elif choice == 5:
sys.exit
else:
print "Wrong choice..."

def output_que():
global left, right, dqueue_arr
while(1):
# menu options for output restricted queue
print "\n1. Insert at right"
print "2. Insert at left"
print "3. Delete from left"
print "4. Display"
print "5 .Quit"
choice = int(raw_input())
if choice == 1:
insert_right()
elif choice == 2:
insert_left()
elif choice == 3:
delete_left()
elif choice == 4:
display_queue()
elif choice == 5:
sys.exit()
else:
print "Wrong choice..."

print "1. Input restricted dequeue"
print "2. Output restricted dequeue"
print "3. Enter your choice: "
choice = int(raw_input())
if choice == 1:
input_que()
elif choice == 2:
output_que()
else:
print "Wrong choice..."

1. Input restricted dequeue
2. Output restricted dequeue
2

1. Insert at right
2. Insert at left
3. Delete from left
4. Display
5 .Quit
1
Input the element for adding in queue:
5

1. Insert at right
2. Insert at left
3. Delete from left
4. Display
5 .Quit
1
Input the element for adding in queue:
10

1. Insert at right
2. Insert at left
3. Delete from left
4. Display
5 .Quit
3
Element deleted from queue is:  5

1. Insert at right
2. Insert at left
3. Delete from left
4. Display
5 .Quit
4
Queue elements :   10
1. Insert at right
2. Insert at left
3. Delete from left
4. Display
5 .Quit

An exception has occurred, use %tb to see the full traceback.

To exit: use 'exit', 'quit', or Ctrl-D.