class MyQueue(object): def __init__(self): self.push_queue = Stack_Node() self.pop_queue = Stack_Node() def enqueue(self, data): if self.pop_queue.size > 0: while self.pop_queue.size > 0: self.push_queue.push(self.pop_queue.pop()) self.push_queue.push(data) def dequeue(self): if self.push_queue.size > 0: while self.push_queue.size > 0: self.pop_queue.push(self.push_queue.pop()) return self.pop_queue.pop() def peek(self): if self.push_queue.size > 0: while self.push_queue.size > 0: self.pop_queue.push(self.push_queue.pop()) return self.pop_queue.peek() def __str__(self): if self.push_queue.size > 0: return self.push_queue.__str__() else: return self.pop_queue.__str__()
def sort_stack(stack): spare_stack = Stack_Node() while not stack.is_empty(): if spare_stack.is_empty(): spare_stack.push(stack.pop()) else: element = stack.pop() while not spare_stack.is_empty() and element > spare_stack.peek(): stack.push(spare_stack.pop()) spare_stack.push(element) return spare_stack
class MyQueue(object): def __init__(self): self.push_queue = Stack_Node() self.pop_queue = Stack_Node() def enqueue(self,data): if self.pop_queue.size > 0: while self.pop_queue.size > 0: self.push_queue.push(self.pop_queue.pop()) self.push_queue.push(data) def dequeue(self): if self.push_queue.size > 0: while self.push_queue.size > 0: self.pop_queue.push(self.push_queue.pop()) return self.pop_queue.pop() def peek(self): if self.push_queue.size > 0: while self.push_queue.size > 0: self.pop_queue.push(self.push_queue.pop()) return self.pop_queue.peek() def __str__(self): if self.push_queue.size > 0: return self.push_queue.__str__() else: return self.pop_queue.__str__()
# 3 - Once we either run out of items or find that the element is < the top element # in the spare stack then we want to push that element onto the spare stack # We continue to repeat this step (hence the while) until the given stack is empty from stack import Stack_Node def sort_stack(stack): spare_stack = Stack_Node() while not stack.is_empty(): if spare_stack.is_empty(): spare_stack.push(stack.pop()) else: element = stack.pop() while not spare_stack.is_empty() and element > spare_stack.peek(): stack.push(spare_stack.pop()) spare_stack.push(element) return spare_stack unsorted_stack = Stack_Node() unsorted_stack.push(1) unsorted_stack.push(2) unsorted_stack.push(8) unsorted_stack.push(3) unsorted_stack.push(9) unsorted_stack.push(4) sorted_stack = sort_stack(unsorted_stack) print sorted_stack print sorted_stack.peek()
# We continue to repeat this step (hence the while) until the given stack is empty from stack import Stack_Node def sort_stack(stack): spare_stack = Stack_Node() while not stack.is_empty(): if spare_stack.is_empty(): spare_stack.push(stack.pop()) else: element = stack.pop() while not spare_stack.is_empty() and element > spare_stack.peek(): stack.push(spare_stack.pop()) spare_stack.push(element) return spare_stack unsorted_stack = Stack_Node() unsorted_stack.push(1) unsorted_stack.push(2) unsorted_stack.push(8) unsorted_stack.push(3) unsorted_stack.push(9) unsorted_stack.push(4) sorted_stack = sort_stack(unsorted_stack) print sorted_stack print sorted_stack.peek()