********please explain as thorough as possible with as many //comments to fully
ID: 3918500 • Letter: #
Question
********please explain as thorough as possible with as many //comments to fully teach this toa person whi has limited knowledge**********
5. Assume that the node of a linked list is in the usual info-link form with the info of type int. (list and ptr are pointers of type nodeType.) The following code creates a linked list: ptr new nodeType ptr->info 16 list-new nodeType list->info 25; list->link-ptr; ptr = new nodeType; ptr->info 12 ptr->link = NULL; list->link->link- ptr; Use the linked list created by this code to answer the following questions. (These questions are independent of each other.) Declare additional poin- ters if you need them a. Which pointer points to the first node of the linked list? b. Determine the order of the nodes of the linked list c. Write a C++ code that creates and inserts a node with info 45 after the node with info 16. d. Write a C++ code that creates and inserts a node with info 58 before the node with info 25. Does this require you to the change the value of the pointer that was pointing to the first node of the linked list? e. Write a C++ code that deletes the node with info 25. Does this require you to the change the value of the pointer that was pointing to the first node of the linked list?Explanation / Answer
ptr = new nodeType;//creating a node named ptr
ptr->info=16;//setting data of ptr t0 116
list = new NodeType;//creating node named list
list->info=25;//setting data to 25
list->link=ptr;//aasigning next of list is ptr. With this node list will be follwed by ptr. That is list is the first node and ptr is the last node at this instance
ptr=new nodeType;//creating a new node and assigning it to ptr
ptr->info=12;//setting data to 12
ptr->link=NULL;//setting its link as NULL
list->link->link=ptr;//and setting this as last node in the list with this statement.
a) As explained above list is the first node of the link.
b) order of nodes are list,list->link and ptr that is 25,16,12
c) newNode = new nodeType;//creating a node named newNode
newNode->info=45//setting data to 45
list->link->link=newNode;//with this link will be create between node 16 and 45
newNode->link=ptr;//with this link will be created between 45 and last node
d)newNode = new nodeType;//creating a node named newNode
newNode->info=58//setting data to 45
newNode->link=list;//setting link ebtwwen first node and newly added node.
Yes need to change pointer pointing to first node with this change. Because previously 25 is the first node in the list and noow we have inserted 58 before the 25 which means
that now 58 becomes the first node and change is needed. So now pointer points to 58 as first node.
e) Now 58 is the first node. Now no need of changing the pointer pointing to first node eventhough we delete the node 25.
Suppose head is the noe pointing to first and list is the node we are deleting now deletion code as follows
head->link=list->link
list=NULL;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.