Question: Question 2 [15 marks] a) Figure B2.1 shows a singly linked list that stores the information for cars. Pointer head points to the first node
![Question 2 [15 marks] a) Figure B2.1 shows a singly linked](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f31d6292af2_80266f31d6227d3f.jpg)


Question 2 [15 marks] a) Figure B2.1 shows a singly linked list that stores the information for cars. Pointer head points to the first node in the list. head Brand Model Year next Brand Model Yeay next Brand Model Year next Toyota Altis 2009 .Nissan Sunny 2011 Honda City 2010 NULL Figure B2.1: Linked-list for Cars 1) Write a class definition of a node for the linked-list in Figure 2.1. Every node should have Brand, Model, Year and next as the attributes. Also, define a pointer variable called head which will point to the first node in the list. [3 marks] ii) Write the source codes that will display all the information of the cars as the output shown in Figure B2.2. You need to use the pointer head in order to print the value. Brand : Toyota Model : Altis Year : 2009 Brand : Nissan Model : Sunny Year : 2011 Brand : Honda Model : City Year : 2010 Figure B2.2 : Output [3 marks] 5 111) Based on your previous class definition in a(1), write the code to create a new node as shown in Figure B2.3, and then assign the following data to the new node: "BMW", "7301", 2015. newNode Year Brand BMW Model 730i next NULL 2015 Figure B2.3: newNode [2 marks] b) Figure B2.4 shows a linked list that has 3 nodes and 4 pointers head, prev, current and tail. item next item next item next item next head 5 6 8 9 prev current tail Figure B2.4 : Linked-list with 3 nodes. The node with the value of 6 is going to be removed from the list. Write the code will delete the node from the list using pointer prev and current. [3 marks] c) Given the code segment in Program B2.1, answer the following question, i until iv. 1 Node *newnode; newnode->item = 2; OWN newNode->next = head; head = newNode; 6 7 8 9 10 11 prev = prev->next; prev->item = 7; tail = tail->next; tail->next = head; Program B2.1 : A code segment in a linked-list i) Draw the state of the linked list in Figure B2.4 after the execution of statement in line 1-2. [1 mark] 11) Draw the state of the linked list in Figure B2.4 after the execution of statement in line 4-5. [1 mark] 111) Draw the state of the linked list in Figure B2.4 after the execution of statement in line 7-8. [1 mark] iv) Draw the state of the linked list in Figure B2.4 after the execution of statement in line 10-11. [1 mark]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
