Define a function length that expects a singly linked structure (the head of the
ID: 3733319 • Letter: D
Question
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.
"""
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()
Explanation / Answer
Please find below is the a7.py file content which have implementation of insert, count and printStructure function.
= a7.py =
from node import Node
def length(head):
"""Returns the number of items in the linked structure
referred to by head."""
probe = head
count = 0
while probe:
count += 1
probe = probe.next
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 is None:
head = Node(newItem)
elif head.data >= newItem:
tmp = head
head = Node(newItem)
head.next = tmp
else:
tmp = head
while ( tmp.next is not None and tmp.next.data < newItem ):
tmp = tmp.next
new = Node(newItem)
new.next = tmp.next
tmp.next = new
return head
def printStructure(head):
while head:
print(head.data)
head = head.next
def main():
"""Gets words from the user and inserts in the
structure referred to by head."""
head = None
userInput = raw_input('Please enter a word (or just hit enter to end): ')
while userInput != '':
head = insert(userInput, head)
userInput = raw_input('Please enter a word (or just hit enter to end): ')
print('The structure contains ' + str(length(head)) + ' items:')
printStructure(head)
if __name__ == "__main__":
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.