Question: Hi, the python program needed to do the following: Define a function length that expects a singly linked structure (the head of the structure) as

Hi, the python program needed to do the following:

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.

Now it does do what it is supposed to, until capital letters are involved. It organizes Ana before algorithm, when it is supposed to organize algorithm before Ana. When it is all small caps, it organizes algorithm before ana. Why does the program do this?

This is the code:

from node import Node

def length(head): probe = head count = 0 while probe: probe = probe.next count +=1 return count

def insert(newItem, head): newNode = Node(newItem) if head == None: head = newNode else: if newItem < head.data: newNode.next = head head = newNode else: probe = head; while probe.next and probe.next.data

def printStructure(head): probe = head answer = "" while probe: answer = answer + probe.data + " " probe = probe.next print (answer)

def main(): 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()

Here is the code for the node:

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

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!