Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the outline below as a starting from __future__ import annotations from typing import Any, Optional class Node: def __init__(self, value: Any, nxt: LinkedList): self.value

image text in transcribed

Use the outline below as a starting

from __future__ import annotations

from typing import Any, Optional

class Node: def __init__(self, value: Any, nxt: LinkedList): self.value = value self.next = nxt

LinkedList = Optional[Node]

def empty_list() -> LinkedList: ...

def add(lst: LinkedList, index: int, value: Any) -> LinkedList: ...

def length(lst: LinkedList) -> int: ...

def get(lst: LinkedList, index: int) -> Any: ...

def setitem(lst: LinkedList, index: int, value: Any) -> LinkedList: ...

def remove(lst: LinkedList, index: int) -> tuple[Any, LinkedList]: ...

- empty_list This function takes no arguments and returns an empty list. - add This function takes a list, an integer index, and another value (of any type) as arguments and places the value at index position in the list (zero-based indexing; any element at the given index before this operation will now immediately follow the new element). If the index is invalid (i.e., less than 0 or greater than the current length), then this operation should raise an IndexError . (Note that an index equal to the length is allowed and results in the new value being added to the end of the list.) This function must return the resulting list. - length This function takes a list as an argument and returns the number of elements currently in the list. - get This function takes a list and an integer index as arguments and returns the value at the index position in the list (zero-based indexing). If the index is invalid (i.e., it falls outside the bounds of the list), then this operation should raise an IndexError. - setitem This function takes a list, an integer index, and another value (of any type) as arguments and replaces the element at index position in the list with the given value. If the index is invalid, then this operation should raise an IndexError. This function must return the resulting list. - remove This function takes a list and an integer index as arguments and removes the element at the index position from the list. If the index is invalid (i.e., it falls outside the bounds of the list), then this operation should raise an IndexError. This function must return a 2-tuple of, in this order, the element previously at the specified index (i.e., the removed element) and the resulting list. 2 Linked List In a file named linked_list.py, provide a data definition for a LinkedList, whose Node class's value field can be any value. An empty list should be represented by the Python value None. Be sure to call your node class Node, so that my tests can create objects correctly. Implement the functions listed above. None of the functions should modify the list that you're given. All of the functions should return a new list. Additionally, implement the eqfunction in your Node class. This will help with testing

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions