Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 20, Problem 2FTE

Explanation of Solution

Purpose of the given code:

The given code is used to displays all the elements in a list “myList”.

Given code:

/* Set a reference "ref" to the first node in the list "myList" */

Node ref = myList; //Line 1

/* This loop will perform up to the reference "ref" is equal to "null" */

while (ref.next != null) //Line 2

{

    /* Display the first node in the "ref" */

    System.out.print(ref.value + " "); //Line 3

/* Move on to the next node by assigning "ref" to the successor of the existing node */

    ref = ref.next; //Line 4

}

Error in the given code:

  • The error is present in “line 2”, in the “while” loop.
    • This error causes a “NullPointerException” if the list is empty and also the code will fail to display the last value in a nonempty list.
  • Therefore, user should replace “ref != null” in the place of “ref.next != null”...

Blurred answer
Students have asked these similar questions
What does the following function do for a given Linked List?  void fun1(struct node* head){if(head == NULL)return;fun1(head->next);printf("%d ", head->data);}
Multiple choice in data structures void doo(node<int>*root){ if(root !=0) { node<int>*p=root; while(root->next!=0) root=root->next; p->data=root->data; } What is this code do? a. swap the first item with the last item in the linked list b. set the first item in the linked list as the last item c. doesn't do anything because the root parameter is passed by value d. change the root item in the binary tree with the farthest leaf item
T/F: All Linked Lists must have head node.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education