You will be writing helper methods for the LinkedList class that we developed and testing them. For
Question:
You will be writing helper methods for the LinkedList class that we developed and testing them.
For this task, assume that the data that you are handling are integers. You may not change the signature of any of these functions. But you may add as many helper functions as needed.
Implement the following methods.
# Get number of links of a singly Linked List. # "Input: [1, 2, 3, 4, 5, 6]" # "Expected: 6" def get_num_links (self):
# Add an item at the beginning of the list def insert_first (self, data):
# Add an item at the end of a list def insert_last (self, data):
# Add an item in an ordered list in ascending order # Assume that the list is already sorted def insert_in_order (self, data):
# Search in an unordered list, return the link if found, None if not found def find_unordered (self, data):
# Search in an ordered list, return the link if found, return None if not found def find_ordered (self, data): |
Please use and finish the following code template:
import random
class Link (object):
# constructor
def __init__ (self, data, next = None):
self.data = data
self.next = next
class LinkedList (object):
# make a linked list
# you may add other attributes
def __init__ (self):
self.first = None
# Get number of links of a singly Linked List.
# "Input: [1, 2, 3, 4, 5, 6]"
# "Expected: 6"
# TODO!
def get_num_links (self):
...
# Add an item at the beginning of the list
def insert_first (self, data):
# add an item at the end of a list
def insert_last (self, data):
# Add an item in an ordered list in ascending order
# Assume that the list is already sorted
def insert_in_order (self, data):
# Search in an unordered list, return None if not found
def find_unordered (self, data):
# Search in an ordered list, return None if not found
def find_ordered (self, data):
# String representation of data 10 items to a line, 2 spaces between data
def __str__ (self):
string = ""
count = 0
curr = self.first
if curr == None:
return string
while curr.next != None:
string += str(curr.data)
count += 1
if count != 10:
string += " "
else:
string += ""
count = 0
curr = curr.next
string += str(curr.data)
return string
# This main function is just for visualization and tests.
def main():
# Test methods insert_first() and __str__() by adding more than
# 10 items to a list and printing it.
linked_List = LinkedList()
for i in range(9, -1, -1):
linked_List.insert_first(i)
print(linked_List)
# Test method insert_last()
linked_List.insert_last(10)
print(linked_List)
# Test method insert_in_order()
linked_List.insert_in_order(5)
print(linked_List)
# Test method get_num_links()
num_links = linked_List.get_num_links()
print(num_links)
# Test method find_unordered()
# Consider two cases - data is there, data is not there
print(linked_List.find_unordered(3))
print(linked_List.find_unordered(11))
# Test method find_ordered()
# Consider two cases - data is there, data is not there
print(linked_List.find_ordered(6))
print(linked_List.find_ordered(12))
if __name__ == "__main__":
main()
Management Accounting Information for Decision-Making and Strategy Execution
ISBN: 978-0137024971
6th Edition
Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young