(i) A linked list is formed from the objects of the class:
class Node
{
int num;
Node next;
}
Write an Algorithm OR a Method to insert a node at the beginning of an existing
linked list.
The method declaration is as follows:
void InsertNode( Nodes starPtr, int n )
(ii) Answer the following questions from the diagram of a Binary Tree given below:
(a) Write the in-order traversal of the above tree structure.
(b) Name the children of the nodes B and G.
(c) State the root of the right sub tree.
Solution:
ALGORITHM
<br>
<br>
Step 1:Start
<br>
Step 2: Node newPtr=new Node()
<br>
Step 3:newPtr.num=n
<br>
Step 4: newPtr.next=NULL
<br>
Step 5:If starPtr= NULL then go to step 7
<br>
Step 6: newPtr.next=starPtr
<br>
Step 7: starPtr=newPtr
<br>
Step 8: End
<br>
<br>
OR
<br>
<br>
Method:
<br>
<br>
<pre class="" style="font-weight: 900;
font-size: medium; font-family: 'Tillana', cursive;height: 15em;">
Class LinkedList
{
void InsertNode(Node starPtr , int n)
{
Node newPtr ;
newPtr = new Node( n , NULL);
if(starPtr = NULL)
newPtr.next = starPtr ;
starPtr =newPtr;
}
}
</pre>
</h5>
<h5 class="text-dark">
(b) (i) D B A F E G H
<br>
(ii) D is the child of B ; Eand Hare the children of G
<br>
(iii) F
</h5>