Answered step by step
Verified Expert Solution
Question
1 Approved Answer
repr ( node 1 ) instead of node 1 . _ - ( ) x + y instead of x * . _ add _
reprnode instead of node
instead of addy
object object instead of objectobject
lencollection instead of collection.
This applies to test cases and functionality within a class. The magic syntax is preferred because it improves
readability. The only common exception is when calling super
Part class LinkedList
In the same files as above, but in different classes LinkedList and TestLinkedList implement the
LinkedList class as described below.
LinkedList
head: Node None
tail: Node None
len: int
init self items: OptionalIterableAny None
Len self int
getheadself Any None
gettailself Any None
addlastself item: Any None
addfirstself item: Any None
removelastself Any
removefirstself Any
Figure : Class diagram for LinkedList. Note that items in LinkedList. is an optional paramter
it defaults to None, and it can be any iterable if included. This means lists, tuples, sets, dictionaries, strings,
or even the range object are fair game.
initself items: Optional IterableAny None
optional collection items are sequentially added to LinkedList if included
use addlast to add items in correct order
getheadself Any I None
Returns item stored in self head, or None for an empty LinkedList
gettailself Any I None
Returns item stored in self tail, or None for an empty LinkedList
addfirstself item: Any None
adds a node to the front of the LinkedList with item
addlastself item: Any None
adds a node to the end of the LinkedList with item
removefirstself Any
removes first node from LinkedList and returns its item
raises a RuntimeError if LinkedList is empty when called
removelastself Any
removes last node from LinkedList and returns its item
raises a RuntimeError if LinkedList is empty when called
Flow:
Empty initialization
Red: testlinkedlist.py TestLinkedList class:
Create empty LinkedList eg LL LinkedList
Assert length is correct should be
Assert gethead and gettail return correct values should be None
Green:
linkedlist.py LinkedList class:
Implement gethead and gettail
addlast
Red: testlinkedlist.py TestLinkedList class:
Create an empty LinkedList
Using a for loop, sequentially add items to end of LinkedList. With each add, assert that
you get the correct values from:
len
gethead
gettail
Green:
linkedlist.py LinkedList class:
Implement addlast
Nonempty initialization
Red: testlinkedlist.py TestLinkedList class:
initialize a new linked list with some iterable, eg:
LL LinkedList abc
LL LinkedList range
Test you get correct values from len, gethead and gettail
Green:
linkedlist.py LinkedList class:
Finish implementing
See note on default parameters below
addfirst
Test similar to addlast
Implement
removefirst
Red: testlinkedlist.py TestLinkedList class:
Build up a linked list either create a nonempty list, or use addfirst or addlast
Iteratively call removefirst until the LinkedList is empty. With every removefirst
call, assert you get correct values from:
removefirst should return item in first node
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started