class Node{ int num; Node next; Node( int n ){ num = n; } }//end Node class clas
ID: 3609854 • Letter: C
Question
class Node{
int num;
Node next;
Node( int n ){
num = n;
}
}//end Node class
class LList{
Node root = null;
public LList join(LList L1, LList L2)
{
LList f = newLList();
Node r1=L1.root;
Node r2=L2.root;
if(r1==null&&r2==null)
f.root=null;
else if(r1==null)
f.root=r2;
else
f.root=r1;
//I am trying to join thetwo list, I am stuck and need some help here.
}//end of join
}//end class LList
class Link{
public static void main( String[] args )
{
LList LL = new LList();
LL.add(1);
LL.add(2);
LList L2 = new LList();
L2.add(7);
L2.add(9);
// Pass L2 to LL's joinmethod
LL.join(L2);
}//end main
}//end class
Explanation / Answer
// I made a lot of changes in your code // Hope this one answers your problem class LList{ Node head; Node tail; public LList() {} public void add(int item) { Node tmp = new Node(item,null); if(head == null) head = tmp; else tail.next = tmp; tail = tmp; } public void show() { Node ptr = head; System.out.println(); while(ptr != null) { System.out.print(ptr.num + " "); ptr =ptr.next; } System.out.println(); } public void join(LList L2) { Node ptr = L2.head; while(ptr!=null){ this.add(ptr.getDatum()); ptr = ptr.next; } } class Node{ int num; Node next; Node(int num, Node next){ this.num = num; this.next = next; } int getDatum() { return num; } } } class Link{ public static void main( String[] args ) { LList LL = new LList(); LL.add(1); LL.add(2); LL.add(3); System.out.println("Elements of the first stack:"); LL.show(); LList L2 = new LList(); L2.add(7); L2.add(8); L2.add(9); System.out.println(" Elements of the secondstack: "); L2.show(); System.out.println(" Joined Elements:"); LL.join(L2); //L2 will be joined withLL LL.show(); } } SAMPLE RUN: Elements of the first stack: 1 2 3 Elements of the second stack: 7 8 9 Joined Elements: 1 2 3 7 8 9 Hope this helps a lot dude! Rate! Rate! Rate!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.