Hello I am having trouble with understanding the circled part in the image provi
ID: 3858468 • Letter: H
Question
Hello I am having trouble with understanding the circled part in the image provided about ordered linked list. for example I imagine the list to look like : inventory -> 10 -> 12 -> 14 -> but what happens if the number entered is larger than all of those numbers? which statement does that belong to in the circled part? if it is the first statement, how and when does prev become NULL? and if inventory = new node would that not bring the number 15 to the beginning in front of 10??
or apace could tbot be al1loeatod for the part. upace E che part already void ingert void) struct pärt "cur, eprev, nex node: new node +malloc (sizeof (struct part)): if (new node ". NULL printE "Database is tul, can printer "Database zeturn; i full; can't add more parts-nih printe( Enter part number: ) scanf (td", nex node->numbe for (eur +inventory, prev cur t- NULL&&new; node number > cur-number prev cur, cur " cur-snext) it feur -NULL &&new; node-snumber - cur->numbez) ( printf("Part already exists. ") free (new node) return princt ("Bnter part name: ) read line (new node-name, NAME LEN) printf (Enter quantity on hand: scanf (d"&new; node-son hand) new node-snext-curi if (prev NULL) inventory new else prev-snext-new nodeExplanation / Answer
In the given code of ordered linked list the main portion is the ‘for loop’ where we have to give emphasis to understand the logic of this program. After taking input of part number it is stored in the new_node’s part number. Then the ‘for loop’ starts. In ‘for loop’ the cur pointer is initialized with the value of the head pointer of the ordered list i.e. inventory; it is the starting point of the list. Here prev pointer is initialized with NULL value. When the cur pointer becomes NULL or new_node’s number is not greater than cur node’s number then only loop stops otherwise loop continues. After each iteration, prev pointer is set to cur pointer’s value and cur pointer is set to cur->next’s value. Thus this ‘for loop’ traverses the whole list. Within the for loop in each iteration, it checks whether the cur node’s number is equal to the new_node’s number or not; if both of them are same then it frees the new_node and returns from the function. So if the return statement within the for loop does not execute that means new_node’s number does not present in the existing list. Then the loop can end in two situations:
i) the cur pointer has become NULL . this also has two possibilities.
A) the loop has completed the full list traversal and all the node’s number is less than the new_node’s number. In this situation the new_node should be added at the last and at this point cur pointer would contain NULL and prev pointer would point to the last node of the list.
B)the list is empty so inventory is NULL so the cur is also NULL as it is initialized with inventory value; in this situation the new_node should be added at the first and at this point cur pointer would contain NULL and prev pointer would also contain NULL.
ii) in second situation, if new_node’s number is not greater than cur node’s number then also loop can be ended. At this point new_node should be added before the cur node as the cur node’s number is greater than new_node’s number. Here prev pointer would point to the node after which new_node should be added. If the cur node points to the first node and if the new_node’s number is less than the first node’s number then the new_node should be added at first; in this case prev pointer would contain NULL.
Basically by this ‘for loop’ the exact position where the new_node should be added, is determined. After execution of this loop prev pointer will contain the value after which the new_node should be inserted and cur pointer would point to the value before which the new_node should be inserted.
Now it should be easier to understand the circled part of the code. If the new number is 15 and it is larger than all the numbers in the list (10,12,14) then it should be situation---- i)A) (see the above explanation part). Here after loop execution cur will contain NULL and prev will point the last node (14). The first line in the circled part would execute and new_node->next would point to cur i.e. NULL (the new_node would be the last node of the list, so new_node->next must be NULL). Next the if condition would not be satisfied here as prev is not NULL; it(prev) is pointed to the node containing 14. So else portion would be executed here and prev->next would be assigned with the value new_node as node containing 14 would now point to the new_node containing 15.
The ‘if condition’ of the circled part would be satisfied only when the new_node insertion position is at first of the list; then only inventory=new_node would be executed. So in the given scenario that would not be executed; here else part would be executed.
(prev ponter is initialized NULL in the for loop statement)
/*Hope this explanation will resolve all queries.*/
/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.