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: "
added_item = int(raw_input())
rear[0] = rear[0]+1
queue_arr.append(added_item)
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"
print "Enter your choice: "
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..."
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: "
item_added = int(raw_input())
self.cqueue_arr.append(item_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"
print "Enter your choice: "
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..."
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: "
added_item = int(raw_input())
# Inputting the element at the right
dqueue_arr.append(added_item)
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:"
added_item = int(raw_input())
# inputting at the left side of the queue
dqueue_arr.insert(added_item,left)
# 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"
print "Enter your choice: "
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..."
# Main menu options
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..."