stack using queue in python code example
Example 1: python list as stack and queue
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk):
print("Underflow")
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
print("Popped item is "+str(item))
def Display(stk):
if isEmpty(stk):
print("Stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print (str(stk[i]))
if __name__ == "__main__":
stk=[]
top=None
Push(stk,1)
Push(stk,2)
Push(stk,3)
Push(stk,4)
Pop(stk)
Display(stk)
Example 2: python list as stack and queue
def enqueue(data):
queue.insert(0,data)
def dequeue():
if len(queue)>0:
return queue.pop()
return ("Queue Empty!")
def display():
print("Elements on queue are:");
for i in range(len(queue)):
print(queue[i])
if __name__=="__main__":
queue=[]
enqueue(5)
enqueue(6)
enqueue(9)
enqueue(5)
enqueue(3)
print("Popped Element is: "+str(dequeue()))
display()