In: Computer Science
Insert: this method takes a value as a parameter and adds a node which contains the value to the end of the linked list
Delete: This method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list
Find: this method takes a value as a parameter, and returns the index of the first node which contains this value. If no nodes are found to contain this value, return False
Reverse: this method reverses the Linked List
Do not edit Lab5.py except to complete the methods required, and to add your name and student number at the top of the file. Any adjustments to the file outside of these will result in grades being deducted.
You do not need to concern yourself with error handling. For example, you can assume that when delete is called, it is called with an appropriate index.
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class LinkedList:
def __init__(self):
self.headval = None
def __str__(self):
node = self.headval
output = "[ "
while(node != None):
output = output + str(node.dataval) + ", "
node = node.nextval
if(len(output) > 2):
output = output[:-2]
return output + " ]"
# DO NOT EDIT ANY CODE ABOVE THIS LINE
def insert(self, val):
# Fill in this method, which takes a value and adds a node which holds the value at the end of the linked list
def delete(self, index=0):
# Fill in this method, which takes an index(with a default value of 0), and deletes the node at the index specified
def find(self, val):
# Fill in this method, which takes in a value and returns the index of the first node which contains that value. If no node containing that value is found, return False
def reverse(self):
# Fill in this method, which reverses the list
#DO NOT EDIT ANY CODE PAST THIS LINE
# Tests
# Insertion
a = LinkedList()
a.insert(1)
a.insert(2)
a.insert('a')
a.insert(3)
print(str(a) == "[ 1, 2, a, 3 ]")
# Deletion
a.delete()
print(str(a) == "[ 2, a, 3 ]")
a.delete(2)
print(str(a) == "[ 2, a ]")
# Find
a.insert('b')
a.insert('c')
a.insert('b')
print(a.find('b') == 2)
print(a.find('c') == 3)
print(a.find('d') == False)
# Reverse
a.reverse()
print(str(a) == "[ b, c, b, a, 2 ]")
Program
Text format:
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class LinkedList:
def __init__(self):
self.headval = None
def __str__(self):
node = self.headval
output = "[ "
while (node != None):
output = output + str(node.dataval) + ", "
node = node.nextval
if (len(output) > 2):
output = output[:-2]
return output + " ]"
# DO NOT EDIT ANY CODE ABOVE THIS LINE
def insert(self, val):
# Fill in this method, which takes a value and adds a node which holds the value at the end of the linked list
new_node = Node(val)
if self.headval is None:
self.headval = new_node
else:
cur = self.headval
while cur.nextval is not None:
cur = cur.nextval
cur.nextval = new_node
def delete(self, index=0):
# Fill in this method, which takes an index(with a default value of 0), and deletes the node at the index specified
if index == 0:
self.headval = self.headval.nextval
else:
cur = self.headval
pre = None
for i in range(index):
pre = cur
cur = cur.nextval
pre.nextval = cur.nextval
def find(self, val):
# Fill in this method, which takes in a value and returns the index of the first node which contains that value. If no node containing that value is found, return False
cur = self.headval
index = 0
while cur!= None and cur.dataval != val:
cur = cur.nextval
index += 1
if cur != None:
return index
return False
def reverse(self):
# Fill in this method, which reverses the list
prev = None
cur = self.headval
while cur is not None:
next = cur.nextval
cur.nextval = prev
prev = cur
cur = next
self.headval = prev
# DO NOT EDIT ANY CODE PAST THIS LINE
# Tests
# Insertion
a = LinkedList()
a.insert(1)
a.insert(2)
a.insert('a')
a.insert(3)
print(str(a) == "[ 1, 2, a, 3 ]")
# Deletion
a.delete()
print(str(a) == "[ 2, a, 3 ]")
a.delete(2)
print(str(a) == "[ 2, a ]")
# Find
a.insert('b')
a.insert('c')
a.insert('b')
print(a.find('b') == 2)
print(a.find('c') == 3)
print(a.find('d') == False)
# Reverse
a.reverse()
print(str(a) == "[ b, c, b, a, 2 ]")
Output:
True
True
True
True
True
True
True
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you.
Thank you.