Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python for the following problem in the picture provided and provide comments # This class represents a single node in the linked list class

Use Python for the following problem in the picture provided and provide comments
# This class represents a single node in the linked list
class Node:
# Constructor
def __init__(self, data, next_node):
# Each Node object keeps track of two things: the data it's holding,
# and a reference to the next node
self.data = data
self.next_node = next_node
# This class represents a linked list
class LinkedList:
# Constructor
def __init__(self):
# The list just needs to keep track of its head node. From there, we
# can get to anywhere else in the list!
self.head = None
# The size attribute keeps track of how many nodes are in the list. We
# need to update this whenever nodes are added or removed.
self.size =0
# __str__ method
def __str__(self):
# This string stores the result to return
result = 'head ->'
# Traverse the list, starting from the head
temp = self.head
while temp != None:
# Add the current node's data to the result
result += f'{temp.data}->'
# Advance temp to the next node in the list
temp = temp.next_node
result += 'None'
return result
# Returns the element at a certain (non-negative) index of the linked list
def get(self, index):
# Make sure that the index is valid
if 0= index = self.size -1:
# Traverse the list to get to that index
temp = self.head
for i in range(index):
temp = temp.next_node
# Return the data stored inside that node
return temp.data
else:
raise IndexError(f'Invalid index provided, index must be between 0 and {self.size -1}')
# Replaces the element at a certain (non-negative) index with a new value
def set(self, index, new_data):
# Make sure that the index is valid
if 0= index = self.size -1:
# Traverse the list to get to that index
temp = self.head
for i in range(index):
temp = temp.next_node
# Update the data of that node with new_data
temp.data = new_data
else:
raise IndexError(f'Invalid index provided, index must be between 0 and {self.size -1}')
# Adds new_data to the head of the list
def add_to_head(self, new_data):
# Create a new node that contains new_data. The new node's next points
# to the current head of the list.
new_node = Node(new_data, self.head)
# Update the list's head reference to point to the new node
self.head = new_node
# We could combine both actions above into a single line:
# self.head = Node(new_data, self.head)
# Update the size attribute of the linked list
self.size +=1
# Adds new_data at the specified index of the list
def add(self, new_data, index):
# If index is 0, call the previously written add_to_head method
if index ==0:
self.add_to_head(new_data)
# Make sure that the index is valid. Valid indices *do* include the
# size of the list, since that would be adding a new node at the end of
# the list.
elif 0 index = self.size:
# Get to the insertion point of the new node
temp = self.head
for i in range(index -1):
temp = temp.next_node
# Create the new node, and change the list references to insert
# that node
new_node = Node(new_data, temp.next_node)
temp.next_node = new_node
# We could combine both actions above into a single line:
# temp.next_node = Node(new_data, temp.next_node)
# Update the size attribute of the linked list
self.size +=1
else:
raise IndexError(f'Invalid index provided, index must be between 0 and {self.size}')
# Deletes the head node from the list, and returns the data that was deleted
def delete_from_head(self):
# Make sure the list isn't empty
if self.head != None:
temp = self.head.data
self.head = self.head.next_node
self.size -=1
return temp
else:
# Handle this however desired - probably should raise some kind of
# exception
pass
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

Describe five general characteristics of the Renaissance period.

Answered: 1 week ago

Question

Did you check photos for quality and rights clearance?

Answered: 1 week ago

Question

Did you check the facts, their accuracy, and sources?

Answered: 1 week ago