/ Published in: C
conversion between queue and stack
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Queue: def __init__(self): self.stack_a = Stack() self.stack_b = Stack() def EnQueue(self, data): self.statck_a.Push(data) def DeQueue(self): if self.stack_b.IsEmpty(): self.stack_a.Lock() for data in self.stack_a: self.stack_b.Push(data) self.stack_a.UnLock() return self.stack_b.Pop() class Stack: def __init__(self): self.queue_a = Queue() self.queue_b = Queue() def Push(self, data): self.queue_a.EnQueue(data) def Pop(self): if self.queue_b.IsEmpty(): self.queue_a.Lock() for i in range(self.queue_b.Length()): self.queue_b.EnQueue(self.queue_a.GetHead()) self.queue_b.UnLock() return self.queue_b.GetTail()