python list as stack and queue

Solutions on MaxInterview for python list as stack and queue by the best coders in the world

showing results for - "python list as stack and queue"
Charley
20 Apr 2019
1def isEmpty(stk): # checks whether the stack is empty or not
2   if stk==[]:
3      return True
4   else:
5      return False
6
7def Push(stk,item): # Allow additions to the stack
8   stk.append(item)
9   top=len(stk)-1
10
11def Pop(stk):
12   if isEmpty(stk): # verifies whether the stack is empty or not
13      print("Underflow")
14   else: # Allow deletions from the stack
15      item=stk.pop()
16      if len(stk)==0:
17         top=None
18      else:
19         top=len(stk)
20         print("Popped item is "+str(item))
21
22def Display(stk):
23   if isEmpty(stk):
24      print("Stack is empty")
25   else:
26      top=len(stk)-1
27      print("Elements in the stack are: ")
28      for i in range(top,-1,-1):
29         print (str(stk[i]))
30
31# executable code
32if __name__ == "__main__":
33   stk=[]
34   top=None
35   Push(stk,1)
36   Push(stk,2)
37   Push(stk,3)
38   Push(stk,4)
39   Pop(stk)
40   Display(stk)
Liya
27 Jul 2016
1#Adding elements to queue at the rear end
2def enqueue(data):
3   queue.insert(0,data)
4
5#Removing the front element from the queue
6def dequeue():
7   if len(queue)>0:
8      return queue.pop()
9   return ("Queue Empty!")
10
11#To display the elements of the queue
12def display():
13   print("Elements on queue are:");
14   for i in range(len(queue)):
15      print(queue[i])
16
17# executable code
18if __name__=="__main__":
19   queue=[]
20   enqueue(5)
21   enqueue(6)
22   enqueue(9)
23   enqueue(5)
24   enqueue(3)
25   print("Popped Element is: "+str(dequeue()))
26   display()
queries leading to this page
building a stack queue in pythonhow to use stack and queue in pythonpython stack queue with arraysstack and queue in python examplepython stack using queuesimplement a queue using two stacks queue using stacks pyhtonis lifo queue in python same as stackimplement stack and queue using pythin liststack using queue python codeimplement queue using stacks pythonstack and queue in python programizqueue using stack python codepython stack queuepython when to use a queue and when to use a stackimplement a queue using two stacks pythondoes python list can be act as both queue and stack 3fimplement stack using queue in pythonpython stacks and queuesimplementation of stack and queue in pythonimplement queue using stack in pythonwhat is stack and queue in pythonexerises python stack and queuestack using queue in pythonwhere are stacks and queues used in pythonpython module for stack and queuequeue stack in pythonpython stack to implement queuepython queue and stackhow to make queue using 2 stacks pythondeque stack pythonpython queue and stack modulehow to implement stack using queue in pythonhow to implement queue as a stack pyhtonwrite a python program to implement a stack and queue using list python stack and queue libraryinbuilt stack and queue in pythonstack queue pythonstacks and queues in python in builtcode stack and queue operations pythonimplement queue using stack pythonstack and queue data structure pythonok implement stack using queue in pythonstack vs queue in pythonstack queue in pythonimplement stack using queue pythonqueue as stack in pythonqueue and stack in pythonqueue on stacks pythonstack and queues pythonstack and queue in pythonpython stack and queuestack in python using queuestacks and queues in pythonstacks and queues pythonstack implementation using queue in pythonpython stack and queue implementationpython list as stack and queue