prep5

.py

School

University of Toronto *

*We aren’t endorsed by this school

Course

148

Subject

Computer Science

Date

Jan 9, 2024

Type

py

Pages

4

Uploaded by EarlPheasant3869

Report
"""CSC148 Prep 5: Linked Lists === CSC148 Fall 2023 === Department of Computer Science, University of Toronto 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. Authors: David Liu and Diane Horton All of the files in this directory and all subdirectories are: Copyright (c) 2020 David Liu and Diane Horton === Module Description === This module contains the code for a linked list implementation with two classes, LinkedList and _Node. NOTE: There is an additional task in the prep5_starter_tests.py file. """ from __future__ import annotations from typing import Any from python_ta.contracts import check_contracts @check_contracts class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. Attributes: - item: The data stored in this node. - next: The next node in the list, or None if there are no more nodes. """ item: Any next: _Node | None def __init__(self, item: Any) -> None: """Initialize a new node storing <item>, with no next node. """ self.item = item self.next = None # Initially pointing to nothing @check_contracts class LinkedList: """A linked list implementation of the List ADT. Private Attributes: - _first: The first node in the linked list, or None if the list is empty. """
_first: _Node | None def __init__(self) -> None: """Initialize an empty linked list. """ self._first = None def print_items(self) -> None: """Print out each item in this linked list.""" curr = self._first while curr is not None: print(curr.item) curr = curr.next ########################################################################## # Part 1 # # For each of the following linked list methods, read its docstring # and then complete its implementation. # You should use the provided *linked list traversal* code template # as your starting point, but of course you should modify it as necessary! # # NOTE: the first two methods are new special methods (you can tell by the # double underscores), and enable some special Python behaviour that we've # illustrated in the doctests. # # At the bottom of this file, we've included some helpers # to create some basic linked lists for our doctests. ########################################################################## def __len__(self) -> int: """Return the number of elements in this list. >>> lst = LinkedList() >>> len(lst) # Equivalent to lst.__len__() 0 >>> lst = three_items(1, 2, 3) >>> len(lst) 3 """ i = 0 curr = self._first while curr is not None: i += 1 curr = curr.next return i def __contains__(self, item: Any) -> bool: """Return whether <item> is in this list. Use == to compare items. >>> lst = three_items(1, 2, 3) >>> 2 in lst # Equivalent to lst.__contains__(2) True >>> 4 in lst False """ curr = self._first
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help