conversion between queue and stack


/ Published in: C
Save to your folder(s)

conversion between queue and stack


Copy this code and paste it in your HTML
  1. class Queue:
  2. def __init__(self):
  3. self.stack_a = Stack()
  4. self.stack_b = Stack()
  5.  
  6. def EnQueue(self, data):
  7. self.statck_a.Push(data)
  8.  
  9. def DeQueue(self):
  10. if self.stack_b.IsEmpty():
  11. self.stack_a.Lock()
  12. for data in self.stack_a:
  13. self.stack_b.Push(data)
  14. self.stack_a.UnLock()
  15.  
  16. return self.stack_b.Pop()
  17.  
  18.  
  19. class Stack:
  20. def __init__(self):
  21. self.queue_a = Queue()
  22. self.queue_b = Queue()
  23.  
  24. def Push(self, data):
  25. self.queue_a.EnQueue(data)
  26.  
  27. def Pop(self):
  28. if self.queue_b.IsEmpty():
  29. self.queue_a.Lock()
  30. for i in range(self.queue_b.Length()):
  31. self.queue_b.EnQueue(self.queue_a.GetHead())
  32. self.queue_b.UnLock()
  33.  
  34. return self.queue_b.GetTail()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.