Hello, This is a Data Structures question. The topic is about STACK Can anyone p
ID: 3847658 • Letter: H
Question
Hello, This is a Data Structures question. The topic is about STACK
Can anyone please help me with this question? Much Appreciated.
Question 1 Consider the following linked structure of integer items. Write a single C++ statement that will achieve each of the following. Note that you can use any of the three pointers i e., Data, ptr1, and ptr2. Moreover, you only need to modify the structure as required, there is no need to free allocated memory. (2 marks) info Data .next The user's Pointer to the data next node Ptrl ptr2 a) Change the node containing 45 to hold 20 b) Test if there is a node after that of value 90 c) Remove the 3rd node from the linked structure d) Remove the last 3 nodes from the linked structureExplanation / Answer
As the list is only singly linked list.. we can't move back. Hence in order to access some node X, we need to start with some node Y, that is coming before X, and then only we can reach to X
a) To Change 45, we need to start from Data pointer.
Moving data pointer twice will make it point to node containing 45.
Data->next->next->info = 20
b) ptr2 points to node with value 80, if we move to next node, then we can check if the next pointer of node containing 90 is null. If it is, it means there is no node after that.
if(ptr2->next->next == NULL)
c) To remove 3rd node, i.e. with value 45, we need to traverse till node containing 30, and then maxe the pointer next of 30, point to ptr1, i.e. with node 60.
Data->next->next = ptr1
d) If we make the next pointer of Node containg 60, as NULL, then the link between node 60 and 65 breaks, and that is similar to removing the last 3 nodes from the list.
So if we start from Data ptr, we can just move till node 60.
ptr1->next = NULL
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.