def llIndex_Value (head, n): pass def llDelete_Last_item(head): pass

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need help with the last two functions in python 

class ListNode:
def __init__(self, data, next=None):
self.data data
self.next = next
def __str__(self):
temp = self
result = []
while temp != None:
result.append(str (temp.data))
temp temp.next
result.append (str (None))
return "->".join(result)
def llSize (head):
number = self.head
count = 0
while (number):
count += 1
number = number.next
return count
def llIndex_Value (head, n):
pass
def llDelete_Last_item (head):
pass
def llReverse (head):
first None
currrent = self.head
while (current is not None):
next = current.next
current.next = first
first current
current = next
self.head = first
return first
Transcribed Image Text:class ListNode: def __init__(self, data, next=None): self.data data self.next = next def __str__(self): temp = self result = [] while temp != None: result.append(str (temp.data)) temp temp.next result.append (str (None)) return "->".join(result) def llSize (head): number = self.head count = 0 while (number): count += 1 number = number.next return count def llIndex_Value (head, n): pass def llDelete_Last_item (head): pass def llReverse (head): first None currrent = self.head while (current is not None): next = current.next current.next = first first current current = next self.head = first return first
Instructions
In basic.py you would be given 4 functions which are to find the size of the linked
list, access a specific item using index value, delete the last item in the linked list
and to reverse the linked list. Your task is to solve the functions and return their
output.
For the delete function, you are meant to return the last item which was deleted
from the linked list.
Constraints/Reminders
• The linked list can be empty. In case where values can't be returned since it is
empty return False.
• Do not change the str function.
Example
Given a linked list_1->4->6->7 and an index of 3
• Your first function would return 4.
• Your second would return 7.
• Your third would return 7.
• Your fourth would return 7->6->4->1 .
Transcribed Image Text:Instructions In basic.py you would be given 4 functions which are to find the size of the linked list, access a specific item using index value, delete the last item in the linked list and to reverse the linked list. Your task is to solve the functions and return their output. For the delete function, you are meant to return the last item which was deleted from the linked list. Constraints/Reminders • The linked list can be empty. In case where values can't be returned since it is empty return False. • Do not change the str function. Example Given a linked list_1->4->6->7 and an index of 3 • Your first function would return 4. • Your second would return 7. • Your third would return 7. • Your fourth would return 7->6->4->1 .
Expert Solution
Step 1c

 I have implemented the question in python as per the specification. I have also added a sample main function to run the provided test case and print the correct output. The code is as follows:

 

class ListNode:
def__init__(self,data,next=None)->None:
self.data=data
self.next=next
 
def__str__(self)->str:
temp=self
result=[]
whiletemp!=None:
result.append(str(temp.data))
temp=temp.next
result.append(str(None))
return"->".join(result)

defllSize(self):
temp=self
count=0
whiletemp!=None:
count+=1
temp=temp.next
returncount
 
defllIndex_Value(self,n):
temp=self
whiletemp!=Noneandn>=0:
ifn==0:
returntemp.data
else:
n-=1
temp=temp.next
returnNone
 
defllDelete_Last_Item(self):
temp=self
prev=self
whiletemp.next!=None:
prev=temp
temp=temp.next
 
prev.next=None
returntemp.data

if __name__ == "__main__":
node7=ListNode(7)
node6=ListNode(6,node7)
node4=ListNode(4,node6)
node1=ListNode(1,node4)

print(node1)
print(node1.llSize())
print(node1.llIndex_Value(3))
print(node1.llDelete_Last_Item())
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY