1. Define a function length that expects a singly linked structure (the head of
ID: 3858193 • Letter: 1
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 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:4.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:4.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
Explanation / Answer
As per the Chegg guidelines we are only allowed to provide first four part of question which is mentioned below :
Please submit rest of the part in a fresh new question.
"""
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
def length(head): # 1st question solution
count = 0
if head.next is None:
return None
else:
if head.next.value == n: # here n is size of linked list
count += 1
countInt(head.next, n, count)
return count
return count
def insert(newItem, head):# 2nd question solution
"""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
def insert(self, item):
new_node = Node(item)
if self.head == None:
self.head = new_node
if self.tail != None:
self.tail.next = new_node
self.tail = new_node
return head
def printStructure(self):# 3rd question solution
"""Prints the items in the structure referred to by head."""
# ADD CODE HERE
node = self.head
while node != None:
print (node.data)
node = node.next
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
# 4th question solution
self.next_node = None
object = LinkedList() // Object of linked list class...please write your class name
object.insert(1)
object.insert(2)
object.insert(3)
object.printStructure( )
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.