Question: Complete the provided Python program, a7.py , with the following functions: Define a function length that expects a singly linked structure (the head of the

Complete the provided Python program, a7.pyComplete the provided Python program, a7.py , with the following functions: Define , with the following functions:

Define a function length that expects a singly linked structure (the head of the structure) as an argument. The function returns the number of items (nodes) in the structure.

Define a function printStructure that expects a linked structure as an argument. The function prints each item in the structure. The function does not return a value.

Define a function insert that inserts an item into a singly linked structure in the correct position, so that the structure is always kept in ascending order (alphabetical). The function expects two arguments: the item and the linked structure (which may be empty). The function returns the modified linked structure.

The function main (provided in a7.py) continually asks the user for a string, and adds it to a linked structure in ascending order, by calling the function insert. When the user enters an empty string, the program calls the function length to display the length of the list, then calls the function printStructure to display the contents of the list, then ends execution. You do not need to modify the function main.

The program uses the Node class available here: node.pya function length that expects a singly linked structure (the head of. Save both files (node.py and a7.py) in the same directory, then modify and test a7.py. You should not modify or submit node.py.

"""

File: a7.py

Assignment 7

Define a length function.

Define a printStructure function.

Define an insert function.

Test the above functions and the Node class.

"""

from node import Node

def length(head):

"""Returns the number of items in the linked structure

referred to by head."""

probe = head

count = 0

# ADD CODE HERE: Count the number of nodes in the structure

return count

def insert(newItem, head):

"""Inserts newItem at the correct position (ascending order) in

the linked structure referred to by head.

Returns a reference to the new structure."""

# if head == None:

# if structure is empty

# ADD CODE HERE

# else:

#Insert newItem in its place (ascending order)

# ADD CODE HERE

return head

def """

File: node.py

Node classes for one-way linked structures and two-way

linked structures.

"""

class Node(object):

def __init__(self, data, next = None):

"""Instantiates a Node with default next of None"""

self.data = data

self.next = nextprintStructure(head):

"""Prints the items in the structure referred to by head."""

# ADD CODE HERE

def main():

"""Gets words from the user and inserts in the

structure referred to by head."""

head = None

userInput = input('Please enter a word (or just hit enter to end): ')

while userInput != '':

head = insert(userInput, head)

userInput = input('Please enter a word (or just hit enter to end):

')

print('The structure contains', length(head), 'items:')

printStructure(head)

if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!