Question: Problem 1 (90 pts): In this problem, you need to implement a LinkedList class. This class will work with the Node class you implemented in



Problem 1 (90 pts): In this problem, you need to implement a LinkedList class. This class will work with the Node class you implemented in HW3. You should implement your LinkedList class in linked_list.py. You need to copy your Node class into the same file. You can update your implementation of Node class if you wish, but it should be unnecessary if you did it correctly in HW3. For example, the linked list [8[88] ' 888 ] will be realized using the following instance objects. [IMPORTANT] Before the last question, LinkedList only has three instance variables first, last, and len, otherwise you may lose most or all of the points. Here first, last reference to Node instance objects (or None), and len is an integer. In the last question, you can introduce new instance variables to help you track. - Let's make the first draft for the initializer of LinkedList. In this first draft, the initializer does not take any non-self parameters. It simply initializes the instance variables first, last, and len to None, None, and 0, respectively. You will modify the initializer later. - (10 pts) Write the function append(self, data) for LinkedList class. For example, suppose self is [12 ' [3], this function will add one new Node to the end, so that it becomes [1 ' ' [3] new_node ], where new_node should store the provided data. Note that - You should tackle "appending to an empty linked list" and "appending to a nonempty linked" seperately. - You should always keep first, last, and len updated, not only for this question but also for the following questions. - (10 pts) Write the function prepend(self, data) for LinkedList class. For example, suppose self is [12 ' [3]], this function will add one new Node to the beginning, so that it becomes [new_node 12 ' [3] ], where new_node should store the provided data. Please note the notes in the above question. - (20 pts) Let's finish writing the initializer of LinkedList. The initializer should look like this: def _-_init__(self, init_list = None) You can assume the argument init_list is either a built-in list or None. You can see init_list has default value None. If init_list is None, the initializer initialize the instance variables first, last, and len to None, None, and 0 , respectively. Otherwise, the initializer should make the corresponding linked list as illustrated in the figure in page 1. You can use the append or prepend function you just defined. - (5 pts) Write a magic method so that you can ask for the length of an instance of a LinkedList in the same way that you ask for the length of a list. LL = LinkedList ([1,1]) print (len(LL)) should result in 2 being printed. - (15 pts) Write a magic method, so that you can test whether two instances of LinkedList are equal. [d1d2,dn] and [D1D2DN] are regarded as equal when n==N, i.e. they have the same length; for all i in [1,2,,n], di == Di. Here == in di==Di is defined in HW3 (i.e. their data are equal). Test example: Lempty = LinkedList () L01 = LinkedList ([0,1]) L02 = LinkedList ([0,1]) L1 = LinkedList ([1]) print(Lempty == L01, L01 == L02, L01 == L1) should print False True False. - (10 pts) Write a magic method so that we can print instances of LinkedList in a human-friendly way. For example: LL=LinkedList([8 ', [8],[8],8, ]) LL . append(1) LL . append (2) LL . append (3) LL.prepend(-1) LL.prepend (-2) LL.prepend(-3) print(LL) should print [3218 ' [8][8]8 ' 123 ]. You may want to use the corresponding magic method for the Node class. - [Reminder] For now, LinkedList only has three instance variables first, last, and len. - (20 pts) Write methods __iter__ and __next__, or using generators, so that LinkedList can be iterated over with a for loop. When you are done, the code: a= LinkedList ([0]) a append(1) a append(2) for n in a: print (n, end = " ") for n in a: print(n, end = " ") should print 012 twice. You should be able to loop over the same LinkedList object as many times as you wish. You can introduce new instance variables to help you track
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
