Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is the output: X Y Z --- Y Z X A I don\'t know how computer get that output

ID: 3886900 • Letter: T

Question


this is the output:
X
Y
Z
---
Y
Z
X
A
I don't know how computer get that output. if someone can explain me technic of reading this code.

public static void main(String[] args) Node t- new Node); Node y = new Node(); Node w = new Node(); t.next = w; titem = "X"; t.next.item = "Y"; yitem = ''Z''; w.next y; Node ptr t; while(ptr != null){ System.out.println(ptr.item); ptr = ptr.next; Node r = w; System.out.println(""); r.next.next t; t.next- new NodeO; t.next.item = "A"; ptr = w; while(ptr != null){ System.out.printin(ptr.item); ptr = ptr.next;

Explanation / Answer

Below is the explanation of the code...

Here on first three lines , we are defining three new nodes t,y,w..

then we are setting t.next = w

i.e. t->w is the present linked list

then we are setting item of t to 'X'

then we are setting t.next.item to 'Y' i.e item of w to 'Y'

then we are setting item of y to 'Z'

and in next line w.next = y

So present lnked list becomes t->w->y->NULL with values X - >Y-> Z

next is while loop which starts from t and check till ptr is Null i.e ends at Y and prints the item..

So out put becomes

X

Y

Z

Next we print --

Now we create a new Node r = w

So linked list comes to be r->w->y->Null

then we are setting r.next.next to t,

r.next is w and w.next is y, So

list becomes

r->w->y->t

now we create next of t as a new node and assign item as 'A' , lets assume it to be s

so List becomes

r->w->y>t->s->Null

Next is while loop starting from w and work till ptr becomes Null i.e. end at s and print values

thus output becomes

Y(w)

Z(y)

X(t)

A(s)

Node that in output above the brackets will not come in output it is just mentioned to help you understand which node is getting printed.

Let me know if you still have issue understanding anything