This exercise asks you to define some functions for manipulating linked structures. You should use the Node and TwoWayNode classes, as defined in this chapter. Create a tester module in the testnode.py file that contains your function definitions and your code for testing them. In the testnode.py file, complete the following: Complete the implementation of the length() function (not len). This function expects a singly linked structure as an argument. Returns the number of items in the structure. To test your program run the main() method in the testnode.py file. Your program's output should look like the following:
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
This exercise asks you to define some functions for manipulating linked structures. You should use the Node and TwoWayNode classes, as defined in this chapter.
Create a tester module in the testnode.py file that contains your function definitions and your code for testing them.
In the testnode.py file, complete the following:
- Complete the implementation of the length() function (not len).
- This function expects a singly linked structure as an argument.
- Returns the number of items in the structure.
To test your program run the main() method in the testnode.py file.
Your program's output should look like the following:
0: 0 5: 5
![3
bil
/>
Instructions
This exercise asks you to define some
functions for manipulating linked
structures. You should use the Node
and TwoWayNode classes, as defined in
this chapter.
Create a tester module in the testnode.py
file that contains your function definitions
and your code for testing them.
In the testnode.py file, complete the
following:
1. Complete the implementation of the
length() function (not len).
o This function expects a singly
linked structure as an argument.
H
node.py
1
2 File: node.py
3
|| || ||
testnode.py
4 Node classes for one-way linked structures and two-way
5 linked structures.ngular Snip
6 """
7
8 class Node(object):
9
10
11
12
13
14
15 class TwoWayNode (Node):
16
def __init__(self, data, next = None):
"""Instantiates a Node with default next of No
+
self.data data
self.next = next
17
18
19
20
21 # Just an empty link
22 node1 = None
def __init__(self, data, previous = None, next =
Node. __init__(self, data, next)
self.previous = previous
>_ Terminal
sandbox $
X
+](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fefc27fca-df46-4944-a77f-e495edbd9f30%2Fa5dac619-3f5a-4111-8761-73d080918018%2Fnim5eh5_processed.png&w=3840&q=75)
![Instructions
This exercise asks you to define some
functions for manipulating linked
structures. You should use the Node
and TwoWayNode classes, as defined in
this chapter.
Create a tester module in the testnode.py
file that contains your function definitions
and your code for testing them.
In the testnode.py file, complete the
following:
1. Complete the implementation of the
length() function (not len).
o This function expects a singly
linked structure as an argument.
Roturns the number of items in
node.py
testnode.py
9
10 from node import Node
11
12 def length(head):
13
14
15
16
"""Returns the number of items in the linked struc
referred to by head. ""
# Write your code here
17
18 def main():
19
20
21
22
23
24
25
26
27
28
29 if __name__ == "__main__": main()
30
"""Tests modifications."""
head None
print("0:", length(head))
+
# Add five nodes to the beginning of the linked st
for count in range(1, 6):
head = Node (count, head)
print("5:", length(head))
>_ Terminal
sandbox $
x
X
+](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fefc27fca-df46-4944-a77f-e495edbd9f30%2Fa5dac619-3f5a-4111-8761-73d080918018%2Fs0npxqi_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)