“””
One implementation each of Stack and Queue.
This code is provided solely for the personal and private use of students
taking the CSC148 course at the University of Toronto. Copying for purposes
other than this use is expressly prohibited. All forms of distribution of
this code, whether as given or with any changes, are expressly prohibited.
This file is:
Copyright (c) 2021 Diane Horton, Jonathan Calver, Sophia Huynh, Maryam Majedi,
and Jaisie Sin.
This module is adapted from the CSC148 Winter 2021 A2 with permission from
the author.
===== Module Description =====
This module contains list-based implementations of the Stack and Queue
ADTs. You are NOT required to use either of these in this assignment,
but we include them for those who wish to do so.
“””
from typing import List, Optional, Any
###############################################################################
# Stacks
###############################################################################
class Stack:
“””A last-in-first-out (LIFO) stack of items.
Stores data in a last-in, first-out order. When removing an item from the
stack, the most recently-added item is the one that is removed.
“””
# === Private Attributes ===
# _items:
# The items stored in this stack. The end of the list represents
# the top of the stack.
_items: List
def __init__(self) -> None:
“””Initialize a new empty stack.”””
self._items = []
def is_empty(self) -> bool:
“””Return whether this stack contains no items.
>>> s = Stack()
>>> s.is_empty()
True
>>> s.push(‘hello’)
>>> s.is_empty()
False
“””
return self._items == []
def push(self, item: Any) -> None:
“””Add a new element to the top of this stack.”””
self._items.append(item)
def pop(self) -> Any:
“””Remove and return the element at the top of this stack.
Raise an EmptyStackError if this stack is empty.
>>> s = Stack()
>>> s.push(‘hello’)
>>> s.push(‘goodbye’)
>>> s.pop()
‘goodbye’
“””
if self.is_empty():
raise EmptyStackError
else:
return self._items.pop()
class EmptyStackError(Exception):
“””Exception raised when calling pop on an empty stack.”””
def __str__(self) -> str:
“””Return a string representation of this error.”””
return ‘You called pop on an empty stack.’
###############################################################################
# Queues
###############################################################################
class Queue:
“””A first-in-first-out (FIFO) queue of items.
Stores data in a first-in, first-out order. When removing an item from the
queue, the most recently-added item is the one that is removed.
“””
# === Private attributes ===
# _items: a list of the items in this queue
_items: List
def __init__(self) -> None:
“””Initialize a new empty queue.”””
self._items = []
def is_empty(self) -> bool:
“””Return whether this queue contains no items.
>>> q = Queue()
>>> q.is_empty()
True
>>> q.enqueue(‘hello’)
>>> q.is_empty()
False
“””
return self._items == []
def enqueue(self, item: Any) -> None:
“””Add
“””
self._items.append(item)
def dequeue(self) -> Optional[Any]:
“””Remove and return the item at the front of this queue.
Return None if this Queue is empty.
(We illustrate a different mechanism for handling an erroneous case.)
>>> q = Queue()
>>> q.enqueue(‘hello’)
>>> q.enqueue(‘goodbye’)
>>> q.dequeue()
‘hello’
“””
if self.is_empty():
return None
else:
return self._items.pop(0)
if __name__ == “__main__”:
import python_ta
python_ta.check_all(config={
‘disable’: [‘E1136’]
})