Question 1: (5 marks)
The given file Q1.py already contains statements to implement a simple program to monitor Drink objects using singly linked list structure. You should write statements to the following functions:
a. f1(): Insert at the beginning of the current list a new Drink (named S1, given in the source code)such that: there is no duplicated Drink's code in the list.
Input: size = 3
Expected result:
2C014, Coca-Cola, 10, 600ml, 12.000
PS021, Pepsi, 10, 390ml, 10.000
MD033, Mirinda, 45, 320ml, 12.000 SP005, Schweppes, 8, 320ml, 10.000
b. f2(): Write your code to insert a new node (given in the source code) before the last node of the current list.
Input: size = 5
Expected result:
PS021, Pepsi, 10, 390ml, 10.000 MD033, Mirinda, 45, 320ml, 12.000
SP005, Schweppes, 8, 320ml, 10.000 2C017, Coca-Cola, 20, 600ml, 15.000
NEWNODE, Sprite, 15, 390ml, 12.000
MD029, Mirinda, 14, 390ml, 18.000
c. f3(): Assuming that there is no duplicated amount in the list, find the node with maximum amount.
Input: size = 5
Expected result:
MD033, Mirinda, 45, 320ml, 12.000
d. f4(): Remove the last node, then sort the linked list in an ascending order according to Drink's price. (if two nodes have the same price, do not swap them)
Input: size = 7 Expected result:
PS021, Pepsi, 10, 390ml, 10.000
SP005, Schweppes, 8, 320ml, 10.000 MD033, Mirinda, 45, 320ml, 12.000
SP002, Bohuc, 18, 320ml, 12.000 2C017, Coca-Cola, 20, 600ml, 15.000
MD029, Mirinda, 14, 390ml, 18.000
e. f5(): Let's define the 'value' of a node as follows: value = amount price. LValue and RValue of a node is the total value of nodes before and after that node (for example: if the list contain 7 nodes,and node K is the third node, so the LValue of K is the total value of two first nodes, and the RValue
of K is the total value of the four last nodes).
Assuming the list contains at least 3 nodes, find the node satisfying that: the difference of LValue
and RValue is the minimum (it also means that: LValue - RValue | is the minimum).
+ 100%
Close