Question
What is the output of the following program? Draw pictures to show the linked list and its references as they change. At each gap in
What is the output of the following program? Draw pictures to show the linked list and its references as they change. At each gap in the code you will draw the linked list and all the references. If a reference is null you can show it on the side pointing to nothing (the ground symbol we use in class works well for this). If there is output you will write that as well. Emphasize the output by drawing a box around it and labeling it OUTPUT. These are just batches of code you create a linked list for. There is no other code provided in this exercise.
public class Cities { public static void main(String args[]) { String arr[] = {"Paris", "Rome", "London"}; Node p,q,r; p = new Node ("Madrid"); p = r; q = null;
1.
for(int i = 0; i < 3; i++) { q = new Node(arr[i]); if(i % 2 == 0) { r.setLink(q); r = q; } else { q.setLink(p); p = q; } }
2.
System.out.println("Answer 1: " + p.getData()); System.out.println("Answer 2: " + q.getData()); System.out.println("Answer 3: " + r.getData());
3.
q = p.getLink(); p.setLink(new Node("Athens", q));
4.
System.out.print("Answer 4: List: "); for (q = p; q != null; q = q.getLink()) { System.out.print(q.getData() + " "); } System.out.println(); } }
5.
6. Rewrite all the output from the program here:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started