From 9de1e9c0e1bfa63f121676e83c58449543104ed6 Mon Sep 17 00:00:00 2001 From: Zhongheng Liu on Homelab Date: Tue, 29 Apr 2025 15:00:38 +0300 Subject: simulated paper 4 test on 29 apr 2025 --- simulation_test/Question3_N24.py | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 simulation_test/Question3_N24.py (limited to 'simulation_test/Question3_N24.py') diff --git a/simulation_test/Question3_N24.py b/simulation_test/Question3_N24.py new file mode 100644 index 0000000..55f20b2 --- /dev/null +++ b/simulation_test/Question3_N24.py @@ -0,0 +1,72 @@ +# initialise linked list structure +global LinkedList +# creating empty data and joining pointers +LinkedList = [[-1, i + 1] for i in range(0, 20)] +# set last element to be null pointer +LinkedList[-1][1] = -1 +global FirstEmpty +global FirstNode +FirstEmpty = 0 +FirstNode = -1 + +def InsertData(): + global FirstEmpty, FirstNode, LinkedList + for i in range(5): + number = int(input("your positive integer number: ")) + while number < 0: + print("invalid, must be positive!") + number = int(input("your positive integer number: ")) + if FirstEmpty != -1: + # the linked list is not full + NextEmpty = LinkedList[FirstEmpty][1] + LinkedList[FirstEmpty][0] = number + LinkedList[FirstEmpty][1] = FirstNode + FirstNode = FirstEmpty + FirstEmpty = NextEmpty + else: + return + +def OutputLinkedList(): + global FirstNode, LinkedList + # assign initial node location + next_node = FirstNode + while next_node != -1: + # outputs the data at the given position + print(LinkedList[next_node][0]) + # updates the next following pointer + next_node = LinkedList[next_node][1] + +InsertData() +OutputLinkedList() + +def RemoveData(target_data): + global FirstNode, FirstEmpty, LinkedList + current_node = FirstNode + next_node = LinkedList[FirstNode][1] + if LinkedList[current_node][0] == target_data: + # the first node needs to be removed + new_first_node = LinkedList[current_node][1] + LinkedList[current_node][1] = FirstEmpty + FirstEmpty = current_node + FirstNode = new_first_node + return + while current_node != -1: + # checks if the immediately succeeding element is target + if LinkedList[next_node][0] == target_data: + if LinkedList[next_node][1] == -1: + # the target is at the end of the list + # terminating linked list with null pointer + LinkedList[current_node][1] = -1 + # put element in empty list + else: + # the target is in the middle of the list + # skip the removed element + LinkedList[current_node][1] = LinkedList[next_node][1] + LinkedList[next_node][1] = FirstEmpty + FirstEmpty = next_node + return # returning here because only matching for one occurrence + current_node = next_node + next_node = LinkedList[next_node][1] +RemoveData(5) +print("After") +OutputLinkedList() \ No newline at end of file -- cgit