Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 a4.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 defined in chapter 4 (available here: node.py. Save both files (node.py and a4.py) in the same directory, then modify and test a4.py. You should not modify or submit node.py.

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

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

""" 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

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

More Books

Students also viewed these Databases questions

Question

4. Not sure? Ask them.

Answered: 1 week ago

Question

What were the reasons for your conversion or resistance?

Answered: 1 week ago