Question

In: Computer Science

Show the output of the following code. Assume the node is in the usual info-link form...

Show the output of the following code. Assume the node is in the usual info-link form with info of the type int. Also, list and ptr are reference variables of the type Node.

list = new Node( );

list.info = 88;

ptr = new Node( );

ptr.info = 41;

ptr.link = null;

list.link = ptr;

ptr = new Node( );

ptr.info = 99;

ptr.link = list;

list = ptr;

ptr = new Node( );

ptr.info = 19;

ptr.link = list.link;

list.link = ptr;

ptr = list;

while(ptr != null)

{

     System.out.println(ptr.info);

     ptr = ptr.link;

}

Group of answer choices

88, 41, 99, 19

19, 41, 88, 99

41, 99, 88, 19

99, 19, 88, 41

Solutions

Expert Solution

I have implemented the code on an java platform after adding some part in the code( to avoid errors).

The modified code and the o/p is given below:

public class HelloWorld{

public static void main(String []args){
  
Node list = new Node( );

list.info = 88;

Node ptr = new Node( );

ptr.info = 41;

ptr.link = null;

list.link = ptr;

ptr = new Node( );

ptr.info = 99;

ptr.link = list;

list = ptr;

ptr = new Node( );

ptr.info = 19;

ptr.link = list.link;  

list.link = ptr;

ptr = list;

while(ptr != null)

{

System.out.println(ptr.info);

ptr = ptr.link;

}


}
}
class Node
{
int info;
Node link;

Node()
{
info = 0;
link=null;
}
}

O/p

99
19
88
41

Since System.out.println() is used the o/p is in different lines, to get them in the same line we have to write System.out.print(). Answer is optn d.

The execution of code and adding nodes go in the following order:

1) 88

2) 88---> 41

3) 99---> 88 ----> 41

4) 99---> 19 ----> 88----> 41

And hence we get the o/p. If you are satisfied with the answer then please upvote it, If you have any doubts please add in the comments section below.


Related Solutions

Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
Show the code how to add a node to the front of a list. Draw the...
Show the code how to add a node to the front of a list. Draw the picture also that goes with the code. NOW Show the code how to add a node to the end of a list. Draw the picture also that goes with the code. EXTRA CREDIT Show the code how to add a node to a specific spot in a list. Draw the picture also that goes with the code.
Perfectly competitive industry model assume the following info Price: $2 Q of output: 1800 Total revenue:...
Perfectly competitive industry model assume the following info Price: $2 Q of output: 1800 Total revenue: ? Total cost: 15700 Total fixed cost:? Total variable cost: 7200 Average total cost: minimum Average variable cost:? MC:? Answer the following questions 1.) total revenue: 2.) total fixed costs 3.) average variable cost 4.) marginal cost 5.) profit or loss? 6.) what should a consultant advise? (Answer a, b, c, d or e) -a,) frisk should do nothing it is already maximizing profits...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
In C++, Complete the Code & Show the output. Schedule the following process using Shortest Job...
In C++, Complete the Code & Show the output. Schedule the following process using Shortest Job First Scheduling algorithm Porcress Burst time Arrival time 1 8 0 2 2 0 3 1 0 4 4 0 Compute the following and show the output a) Individual Waiting time & Turnaround time b) Average Waiting time & Turnaround time c) Display the Gantt chart (Order of Execution)    #include using namespace std; //structure for every process struct Process { int pid; //...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
* Show the output of the following BASH code: i=7 i=$((i-5)) if [ $i -eq 4...
* Show the output of the following BASH code: i=7 i=$((i-5)) if [ $i -eq 4 ] # -eq is == then         echo "Four" elif [ $i -eq 2 ] then         echo "Two" else         echo $i fi
Write the XHTML code for a short Web page. Include the following along with the usual...
Write the XHTML code for a short Web page. Include the following along with the usual HTML and BODY tags. You can use whatever hyperlinks and image URLs you want. (other than distasteful sites, that is!) A title Email link A graphic (gif or jpg) Hyperlink(s) to other sites. Heading A list (can be unnumbered, numbered, etc…) A background color or a background image. please do not copy the previous answer.
Write the XHTML code for a short Web page. Include the following along with the usual...
Write the XHTML code for a short Web page. Include the following along with the usual HTML and BODY tags. You can use whatever hyperlinks and image URLs you want. (other than distasteful sites, that is!) A title Email link A graphic (gif or jpg) Hyperlink(s) to other sites. Heading A list (can be unnumbered, numbered, etc...) A background color or a background image.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT