Question: 1. Define a function length that expects a singly linked structure (the head of the structure) as an argument. The function returns the number of

1. 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.

2. 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.

3. 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 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 lengthto 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.

Define this main File.py """ File: a4.py

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 printStructure(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()

The program uses the Node class:

"""

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 = next

Sample output 1: >>> RESTART: C:\a4.py Please enter a word (or just hit enter to end): bottle Please enter a word (or just hit enter to end): a Please enter a word (or just hit enter to end): water Please enter a word (or just hit enter to end): of Please enter a word (or just hit enter to end): The structure contains 4 items: a bottle of water

Sample output 2 (note: items are strings, so lexicographical order is ok): >>> RESTART: C:\a4.py Please enter a word (or just hit enter to end): 5 Please enter a word (or just hit enter to end): 10 Please enter a word (or just hit enter to end): 20 Please enter a word (or just hit enter to end): 15 Please enter a word (or just hit enter to end): 1 Please enter a word (or just hit enter to end): 8 Please enter a word (or just hit enter to end): The structure contains 6 items: 1 10 15 20 5 8

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!