Stack: ====== ==> a linear structure going to work with the principle "LIFO (Last In and First Out) # Stack class Stack: def __init__(self): self.items = list() # added an empty list def push(self, element): # appending of elements into an empty list self.items.append(element) def pop(self): return self.items.pop() def peek(self): return self.items[-1] # rightmost element in the list (Last element of list) def is_empty(self): return self.items == list() s = Stack() print("The Given List = ",s.items) # adding of element to the list s.push('python ') s.push('programming ') s.push('language') print(s.items) print(s.peek()) ret_obj = s.pop() print("The Return Object = ",ret_obj) print(s.peek()) ======================================================== Queue: ====== ==> A linear structure like stack ==> based on principle "FIFO (First In and First Out)" Implementing the Queue using List: ================================== # Queue with List data Queue = list() # empty list Queue.append(10) Queue.append(20) Queue.append(30) Queue.append(40) Queue.append(50) print("The Queue Before the Deque Operation is = ") print(Queue) # Deque Operation (Removing the first element) [10,20,30,40,50] ret1 = Queue.pop(0) # First In [20,30,40,50] ret2 = Queue.pop(0) # [30,40,50] ret3 = Queue.pop(0) # [40,50] ret4 = Queue.pop(0) # [50] ret5 = Queue.pop(0) # [] print(ret1) print(ret2) print(ret3) print(ret4) print(ret5) print("The Queue after the DeQue Operation is = ") print(Queue) ========================================== Deque: ===== Double Ended Queue is a generalization of stacks and Queues. Which it supports: memory Efficiency fast append pops elements from either side. pop() ==> remove/pop from last pop(0) ==> remove/pop from beginning/start # Deque from collections import deque # Create a deque d = deque(['a', 'e', 'i', 'o', 'u']) print("The Given Deque = ",d) # Appending to the Deque at the right d.append('A') d.append('E') d.append('I') d.append('O') d.append('U') print("After the Appending Operation the Deque is = ") print(d) # pop from left # popleft() ==> it can pop the left most element d.popleft() print("The Deque After the left pop operation is = ") print(d) # pop from right # pop() ==> it can pop the right most element d.pop() print("The Deque After the right pop operation is = ") print(d) # ld = [1,2,3,4,5,6,7] # ld.popleft() ============================================= Queue Implementation with collections.deque: ============================================ # Queue with Deque from collections import deque queue = deque() # empty queue print("The Queue without Enque = ",queue) #Enque the elements into the above definition queue.append('a') queue.append('e') queue.append('i') queue.append('o') queue.append('u') print("The Given Queue = ",queue) # Deque from left print(queue.popleft()) print("The Queue after the left pop operation is = ",queue) # Deque from right print(queue.pop()) print("The Queue after the right pop operation is = ",queue) ============================================ Queue Implementation with Queue() ================================= from queue import Queue q = Queue(maxsize=7) print("The Length of the Queue = ",q.qsize()) # Enque Operation # q.append(10) q.put(10) q.put(20) q.put(30) print("The Length of the Queue = ",q.qsize()) q.put(40) q.put(50) q.put(60) q.put(70) print("The Length of the Queue = ",q.qsize()) # q.put(1234) # # print("The Length of the Queue = ",q.qsize()) # Deque Operation # q.pop() res1 = q.get() res2 = q.get() print(res1) print(res2) print(q.get()) print(q.get()) print(q.get()) print(q.get()) print(q.get()) # print(q) # is queue is empty? print(q.empty())