Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help to write the function body for chop_front class _Node: A node in a linked list. Note that this is considered a private class,

image text in transcribed

image text in transcribed

image text in transcribed

please help to write the function body for chop_front

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: OptionalC_Node] def __init__(self, items: list) -> None: ""'"Initialize a new linked list containing the given items. The first node in the linked list contains the first item in . if items [] : # No items, and an empty list! self._first = None else: self._first = _Node(items[0]) curr = self._first for item in items[1:]: curr.next = _Node(item) curr = curr.next def is_empty(self) -> bool: """Return whether this linked list is empty. >>> LinkedList([]).is_empty True >>> LinkedList([1, 2, 3]). is_empty() False return self._first is None def _str__(self) -> str: """Return a string representation of this list in the form [iteml -> item2 -> ... > item-n]'. | >>> str(LinkedList([1, 2, 3])) [1 -> 2 -> 3]' >>> str(LinkedList(o)) 'I' HHH! def chop_front(self, n: int) -> int: "Remove the first n nodes from this linked list, if possible. If n > the length of this linked list, remove all the nodes. Return the number of nodes that were removed. precondition: n >= 1 >>> linky = Linkedlist([1, 2, 3, 4, 5, 6, 7, 8]) >>> linky.chop_front(3) >>> print(linky) [4 -> 5 -> 6 -> 7 -> 8] >>> linky = LinkedList([22, 1, 5, 4, 7, 4]) >>> linky.chop_front(13) >>> print(linky) 3 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions