def test_stack(self): my_stack = Stack() numbers = [1, 2, 3, 4, 5, 6, 7, 8] # Push all the numbers in the numbers list in to my_stack for number in numbers: my_stack.push(number) # The stack shouldn't be empty self.assertFalse(my_stack.is_empty()) # The element at the head of the stack should be 8 self.assertEqual(my_stack.peek(), 8) # Pop 6 elements out last_element = None for number in range(0, 6): last_element = my_stack.pop() # The last element popped out should be 3 self.assertEqual(last_element.getData(), 3) # The element at the head of the stack should be 2 self.assertEqual(my_stack.peek(), 2) # Empty the stack for number in range(0, my_stack.size()): my_stack.pop() # The stack should now be empty self.assertTrue(my_stack.is_empty())
def dequeue_specific(self, type): # Initialise a temporary stack to hold the animals that are dequeued whilst searching for the specific animal # type temp = Stack() # Continually dequeue animals and push them in to the aforementioned Stack until the intended type of animal # is encountered while self.inspect_tail()[0] != type: temp.push(self.dequeue_any()) dequeued = self.dequeue_any() # Now pop all the animals from temp and enqueue back in to the Shelter while temp.size() > 0: new_animal = temp.pop().getData() self.enqueue(new_animal.get_data()[0], new_animal.get_data()[1]) self.size -= 1 return dequeued
class MyQueue: def __init__(self): self.main = Stack() self.temp = Stack() def put(self, data): while not self.main.is_empty(): self.temp.push(self.main.pop().getData()) self.main.push(data) while not self.temp.is_empty(): self.main.push(self.temp.pop().getData()) def get(self): return self.main.pop() def size(self): return self.main.size() def is_empty(self): return self.main.is_empty()