Show what is produced by the following C++ code. Assume that the node is in usua
ID: 3572473 • Letter: S
Question
Show what is produced by the following C++ code. Assume that the node is in usual inf o-link form with info of the type int. (list and ptr are pointers of the type nodeType.) list = new nodeType; list rightarrow info = 20; ptr = new nodeType; ptr rightarrow info = 28; ptr rightarrow link = HULL; list rightarrow link = ptr; ptr = new nodeType; ptr rightarrow info = 30; ptr rightarrow link = list; list = ptr; ptr = new nodeType; ptr rightarrow info = 42; ptr rightarrow link = list rightarrow link; list rightarrow link = ptr; ptr = list; while(ptr 1= HULL) {coutExplanation / Answer
//Allocating memory for list
list = new nodeType;
//Assigning data 20 to node list
list->info = 20;
//Allocating memory for ptr
ptr = new nodeType;
//Assigning data 28 to node ptr
ptr->info = 28;
//Making link of node ptr to point NULL
ptr->link = NULL;
//Making link of node list point to node ptr
// Now list is 20 -> 28
list->link = ptr;
//Allocating memory for ptr
ptr = new nodeType;
//Assigning data 30 to node ptr
ptr->info = 30;
// Making link of node ptr to point node list
// Now list is 30 -> 20 -> 28
ptr->link = list;
// Updating list to ptr
// Now ptr is 30 -> 20 -> 28
list = ptr;
//Allocating memory for ptr
ptr = new nodeType;
//Assigning data 42 to node ptr
ptr->info = 42;
//Updating link of node list
//42 -> 20 -> 28
ptr->link = list->link;
//Updating link node of list
list->link = ptr;
//Updating ptr
ptr = list;
//List is : 30 -> 42 -> 20 -> 28
//Now the following loop prints : 30 42 20 28
while(ptr != NULL)
{
cout << ptr->info <<endl;
ptr = ptr->link;
}
Output will be:
30
42
20
28
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.