Page 544 - Computer science 868 Class 12
P. 544
Ans. Method Stack Output
push(51) 51
27
push(27) 51
push(5) 5
27
51
pop() 27 5
51
pop() 51 27
push(18) 18
51
push(72) 72
18
51
pop() 18 72
51
5. Write an algorithm and Java code to split a linked list using void split(Node startA, int pos) where start denotes the start
pointer and pos the position of split.
Ans. Algorithm
Step 1: Start
Step 2: Create new Nodes ptr and startB and initialise them to startA and null respectively
Step 3: Initialise c to 1
Step 4: Repeat Step 5 to Step 6 while c < pos
Step 5: Assign ptr = ptr.next
Step 6: Increment c by 1
Step 7: Set startB= ptr.next
Step 8: Set ptr.next=null
Java Code
void split(Node startA, int pos)
{
Node ptr=startA; // initialise prt to start
Node startB=null
int c=1;
while(c<pos) // end of linked list
{
ptr=ptr.next;
c=c+1;
}
// merging the two linked lists by assigning startB to the last node
startB=ptr.next;
ptr.next=null;
}
542542 Touchpad Computer Science-XII

