Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 19, 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
Removing the first node in a linked chain follows the same proceedure as to remove a node on the interior of the list.  True  False
implement this method: numOccurrencesRec(LNode node, int n, int key) – This method takes as parameters a reference to the head of a linked list, a position specified by n, and a key. It returns the number of occurrences of the key in the linked list beginning at the n-th node. If n = 0, it means you should search in the entire linked list. If n = 1, then you should skip the first node in the list.
In a doubly-linked list, each node contains (         ) pointer(s). Group of answer choices 0 1 2 3
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