Advertisement
Sunilsai

Queue_Implementation_Array

May 23rd, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. class Queue:
  2.     def __init__(self, Size):
  3.         self.Size = Size
  4.         self.QueueLis = [None]*self.Size
  5.         self.Front = 0
  6.         self.Rear = 0
  7.         self.Count = 0
  8.        
  9.     def Push(self, Val):
  10.         if(self.Count < self.Size):
  11.             self.QueueLis[self.Rear % self.Size] = Val
  12.             self.Rear += 1
  13.             self.Count += 1
  14.         else:
  15.             print("Queue Is Full")
  16.    
  17.     def Top(self):
  18.         print(self.QueueLis[self.Front % self.Size])
  19.    
  20.     def Pop(self):
  21.         self.Count -= 1
  22.         self.QueueLis[self.Front % self.Size] = None
  23.         self.Front += 1
  24.    
  25.     def PrintQueue(self):
  26.         print(self.QueueLis)
  27.    
  28. ObjQueue = Queue(5)
  29. ObjQueue.Push(1)
  30. ObjQueue.Push(2)
  31. ObjQueue.Push(3)
  32. ObjQueue.Push(4)
  33. ObjQueue.Push(5)
  34. ObjQueue.PrintQueue()
  35. ObjQueue.Push(6)
  36. ObjQueue.Top() # 1
  37. ObjQueue.Pop() # Pop 1
  38. ObjQueue.PrintQueue()
  39. ObjQueue.Top() # 2
  40. ObjQueue.Push(6)
  41. ObjQueue.Pop() # Pop 2
  42. ObjQueue.PrintQueue()
  43. ObjQueue.Top() # 3
  44. ObjQueue.Push(7)
  45. ObjQueue.PrintQueue()
  46. ObjQueue.Pop() # Pop 3
  47. ObjQueue.Push(8)
  48. ObjQueue.Pop()
  49. ObjQueue.Push(9)
  50. ObjQueue.PrintQueue()
  51. #ObjQueue.Pop()
  52. ObjQueue.Top()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement