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!
- Module Description - 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. === Attributes ses 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, Eclass 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. # curr : cursor pointing to nodes during the run e _first: OptionalC_Node] def __init_(self) -> Nond. ***Initialize an empty linked List. self._first = None def print items(self) -> 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 # 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 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: def _len_(self) -> int: "Return the number of elements in this list. >>> 1st = LinkedList(), >>> len(1st) # Equivalent to lst._len_(), >>> lst = three_items(1, 2, 3), >>> len(1st) # TODO: implement this method # curr = self. first # while curr is not None: # ... curr.item ... I # curr = curr.next def contains (self, item: Any) -> bool: ""Return whether sitem> is in this list. 'Use == to compare items. >>> 1st = three items (1, 2, 3), in let # Cancivalent to let contains def _contains_(self, item: Any) -> bool: ""Return whether
- >> 1st = three_items(1, 2, 3) >>> 2 in 1st True >>> 4 in 1st 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'll first need to iterate to the # last node* in the linked list. (Review this prep's Quercus quiz!), def append(self, item: Any) -> None: UKRULISE def append(self, item: Any) -> None: ***"Append
- to the end of this list. >>> 1st = LinkedList() >>> lst.append(1) >>> Ist. first.item >>> lst.append(2) >>> Ist._first.next. item # 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)-> LinkedList: *****Return a linked list containing the given item. 1st - LinkedList() 245 Lil Redust 146 node = _Node(x) 1st._first = node return lst 148 149 158 151 152 153 154 Edef three_items(x1: Any, x2: Any, x3: Any) -> LinkedList: "*"Return a linked list containing the given three items. *** 1st = LinkedList() nodel = _Node(x1) node2 = _Node(x2) node3 = _Node(x3) node1.next = node2 node2.next = node3 1st._first = node1 return ist 156 158 161 162 163 Eif 164 265 _name__ == '_main_': import python_ta python_ta.check_all(config={ "allowed-io': ['print_items'], 'disable': ['W0212') 167 168 }