Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

from __future__ import annotations from typing import Any, Optional class _Node: A node in a linked list. Note that this is considered a private class,

from __future__ import annotations from typing import Any, Optional 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: Optional[_Node] def __init__(self, item: Any) -> None: """Initialize a new node storing , with no next node. """ self.item = item self.next = None # Initially pointing to nothing 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: Optional[_Node] 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 def __len__(self) -> int: """Return the number of elements in this list. >>> lst = LinkedList() >>> len(lst) # Equivalent to lst.__len__() 0 >>> lst = LinkedList() >>> node1 = _Node(1) >>> node2 = _Node(2) >>> node3 = _Node(3) >>> node1.next = node2 >>> node2.next = node3 >>> lst._first = node1 >>> len(lst) 3 """ # TODO: implement this method # curr = self._first # while curr is not None: # ... curr.item ... # curr = curr.next def __contains__(self, item: Any) -> bool: """Return whether  is in this list. Use == to compare items. >>> lst = LinkedList() >>> node1 = _Node(1) >>> node2 = _Node(2) >>> node3 = _Node(3) >>> node1.next = node2 >>> node2.next = node3 >>> lst._first = node1 >>> 2 in lst # Equivalent to lst.__contains__(2) True >>> 4 in lst False """  # TODO: implement this method # curr = self._first # while curr is not None: # ... curr.item ... # curr = curr.next def append(self, item: Any) -> None: """Append  to the end of this list. >>> lst = LinkedList() >>> lst.append(1) >>> lst._first.item 1 >>> lst.append(2) >>> lst._first.next.item 2 """  # TODO: implement this method # curr = self._first # while curr is not None: # ... curr.item ... # curr = curr.next if __name__ == '__main__': import python_ta python_ta.check_all(config={ 'allowed-io': ['print_items'] }) import doctest doctest.testmod()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Which symptoms of ASPD did Bill have?

Answered: 1 week ago