QUESTION

Text
Image


$m=$ Module Description =ux This module contains the code for a linked list implementation with two classes, LinkedList and Node. 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. $==\infty$ Attributes $==$ item: The data stored in this node. next: I The next node in the list, or None if there are no more nodes. item: Any next: Optional[_Node]


def _init_(self, item: Any) $\rightarrow$ None: "" Initialize a new node storing 〈item», with no next node. self ,item $=$ item self.next $=$ None \# Initially pointing to nothing self.item $=$ item self.next = None \# Initially pointing to nothing class LinkedList: $"$ "A Linked List implementation of the List ADT. \# =n= Private Attributes $===$ \#_first: The first node in the linked list, or None if the list is empty. \# curr : cursor pointing to nodes during the run _first: Optional[_Node] def _init_(self) $\rightarrow$ Nont. s" Initialize an empty linked list. self._first = None def__init__(self) $\rightarrow$ Nont: "" Initialize an empty linked list. self._first $=$ None def print items(self) $\rightarrow$ None:


def print_items(self) $\rightarrow$ None: "" "Print out each item in this linked list. "m." curr = self._first while curr is not None: print (curr.item) curr $=$ curr, next \# Prep 5 exercises \# For each of the following linked list methods, read its docstring \# and the complete its implementation. \# You should use as your starting point our *linked list traversal* \# code template, 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 doctIsts. \# At the bottom of this file, we've included some helpers \# to create some basic linked lists for our doctests. def _len_(self) $\rightarrow$ int:


def _len_(self) $\rightarrow$ int: "" "Return the number of elements in this list. $\gg$ lst = LinkedList () $\gg$ len(lst) \# Equivalent to lst._len_() 0 $\gg>$ lst = three_items $(1,2,3)$ $\gg$ len(lst) 3 "n " \# TODO: implement this method \# curr = self._first \# while curr is not None: \# ... curr.item ... \# curr $=$ curr.next def _contains_(self, item: Any) $\rightarrow$ bool: "n"Return whether <item> is in this list. Use $==$ to compare items. $\gg>$ lst $=$ three_items $(1,2,3)$


def _contains_(self, item: Any) $\rightarrow$ bool: "n"Return whether 〈item> is in this list. Use $==$ to compare items. $\gg 1$ st = three_items $(1,2,3)$ 〉) 2 in lst True \# Equivalent to 1st._contains_(2) $\gg>4$ in lst False \# TODO: implement this method \# curr = self._first \# while curr is not None: \# ... curr.item ... \# curr $=$ curr. next \# HINTS: for this one, you'll be adding a new item to a linked list. \# 1. Create a new _Node object first. \# 2. Consider the cases where the list is empty and non-empty separately. 3. For the non-empty case, you'11 first need to iterate to the *1ast node* in the linked list. (Review this prep's Quercus quiz!) def append(self, item: Any) $\rightarrow$ None:


UnkedList def append(self, item: Any) $\Rightarrow$ None: "n"Append <item> to the end of this list. $\gg>1$ st = LinkedList ( ) 〉> 1st.append(1) 〉 lst. first.item 1 \>1st.append(2) 〉 lst. first.next.item 2 " "n \# TODO: implement this method \# curr = self._first \# while curr is not None: \# … curr.item ... curr $=$ curr. next \# Helpers for creating linked lists (testing purposes only) def one item (x: Any) $\rightarrow$ Linked List: "n"Return a linked List containing the given item.""r" 1st = LinkedList()


node $=$ Node $(x)$ lst._first $=$ node return lst def three_items(x1: Any, x2: Any, x3: Any) $\rightarrow$ LinkedList: "neturn a linked list containing the given three items." "n" lst $=$ LinkedList ( ) node1 $=$ Node $(x 1)$ node $2=$ Node $(\times 2)$ node $3=$ Node $(\times 3)$ node 1 . next $=$ node 2 node2. next $=$ node 3 lst._first $=$ node1 return lst if _ name__ $==$ '_ main_ ': import python_ta python_ta.check_all(config $=\{$ 'allowed-io': ['print_items'], 'disable': ['W0212'] 3)

we have to complete the #TODO COMMENT and read grey instructions. if you could show me code structure on python, that would be great !
thank you!

Public Answer

0LZ6TF The First Answerer