I am trying ro run this code and Getting error in attach(below) pic here. Was su
ID: 3861766 • Letter: I
Question
I am trying ro run this code and Getting error in attach(below) pic here. Was supposed to define the Three Fucntion as Below.
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.
File a4.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
if head.next is None:
return None
else:
if head.next.value == n:
count += 1
countInt(head.next, n, count)
return count
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 item
def printStructure(self):
"""Prints the items in the structure referred to by head."""
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()
File: Node.py
"""
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
import sys
sys.path.append
from node import Node
class None(length)
class Node(object):
def __init__(self, data, next =None):
self.data = data
self.data = next
removedItem = head.data
head = head.next
return removedItem
if head is None or index <0:
head = None(newItem, head)
else:
probe = probe
while index >1 and probe.next!=None:
probe = probe.next,
index -= 1
probe.next = None(newItem, probe.next)
while probe != None:
print probe.data
probe = probe.next
newNode = Node
if head is None:
head = newNode
else:
probe = head
while probe.next.next != None:
probe = probe.next
probe.next = newNode
probe = head
print "linked structure after inserting end"
while probe != None:
print probe.data
probe = probe.next
if index <=0 or head.next is None
removedItem = head.data
head = head.next
return removedItem
else:
probe = head
while index >1 aand probe.next.next != None:
probe = probe.next
index -= 1
removedItem = probe.next.data
probe.next = probe.next.next
return removedItem
I have implemented only the basic code which is the first requirement as suggested by Chegg to leave the rest if question is very big
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.