Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
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”...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents 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.
Chapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Ch. 20.1 - Prob. 20.1CPCh. 20.1 - Prob. 20.2CPCh. 20.3 - Prob. 20.4CPCh. 20 - A list is a collection that _____. a. associates...Ch. 20 - Prob. 2MCCh. 20 - Prob. 3MCCh. 20 - Prob. 4MCCh. 20 - Prob. 5MCCh. 20 - Prob. 6MCCh. 20 - Prob. 7MC
Ch. 20 - Prob. 11TFCh. 20 - Prob. 12TFCh. 20 - Prob. 13TFCh. 20 - Prob. 14TFCh. 20 - Prob. 15TFCh. 20 - Prob. 16TFCh. 20 - Prob. 17TFCh. 20 - Prob. 18TFCh. 20 - Prob. 29TFCh. 20 - Prob. 20TFCh. 20 - Prob. 1FTECh. 20 - Prob. 2FTECh. 20 - Prob. 3FTECh. 20 - Prob. 4FTECh. 20 - Prob. 5FTECh. 20 - Prob. 1AWCh. 20 - Prob. 2AWCh. 20 - Prob. 3AWCh. 20 - Prob. 4AWCh. 20 - Prob. 3SACh. 20 - Prob. 4SACh. 20 - Prob. 5SACh. 20 - Consult the online Java documentation and...Ch. 20 - Prob. 1PCCh. 20 - Prob. 2PC
Knowledge Booster
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
- Complete the //comments and TODOs in c++ #include "LinkedList.h" using namespace std; // Add a new node to the list void LinkedList::insert(Node* prev, int newKey){ //Check if head is Null i.e list is empty if(head == NULL){ head = newNode; head->key = newKey; head->next = NULL; } // if list is not empty, look for prev and append our node there elseif(prev == NULL) { Node* newNode = newNode; newNode->key = newKey; newNode->next = head; head = newNode; } else{ Node* newNode = newNode; newNode->key = newKey; newNode->next = prev->next; prev->next = newNode; } } // TODO: SILVER PROBLEM // Delete node at a particular index bool LinkedList::deleteAtIndex(int n) { boolisDeleted = false; if(head == NULL){ cout<<"List is already empty"<<endl; returnisDeleted; } // Special case to delete the head if (n == 0) { //TODO } Node *pres = head; Node *prev = NULL; // TODO returnisDeleted; } // TODO: GOLD PROBLEM // Swap the first and last nodes (don't…arrow_forward3. void insert (Node newElement) or def insert(self, newElement) (4) Pre-condition: None. Post-condition: This method inserts newElement at the tail of the list. If an element with the same key as newElement already exists in the list, then it concludes the key already exists and does not insert the key.arrow_forwardint F(node<int>&p){int c=0; while(p!=0){p=p->next; c++; } return c;} This function is a. return the number of items in the linked list b. return the number of items in a linked list and destroy the linked list c. None of these d. destroy the list and free all allocated nodesarrow_forward
- When removing a node from a linked list, what are the two steps?arrow_forwardGiven the declarations struct NodeType{ int data; NodeType* link;};NodeType* headPtr; // External pointer to a linked listNodeType* p;Write the code that would sum all the elements in the list.arrow_forwardusing namespace std; class SinglyLinkedListNode { // INSERT YOUR CODE HERE }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } voidinsert_node(intnode_data) { // INSERT YOUR CODE HERE } }; void free_singly_linked_list(SinglyLinkedListNode* node) { // INSERT YOUR CODE HERE } // Complete the has_cycle function below. /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ bool has_cycle(SinglyLinkedListNode* head) { SinglyLinkedListNode* temp = head; bool isCycle = false; while (temp != nullptr) { // INSERT YOUR CODE HERE } } int main() { // INSERT YOUR CODE HERE TO TEST YOUR CODE return0; }arrow_forward
- using namespace std; class SinglyLinkedListNode { // INSERT YOUR CODE HERE }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } voidinsert_node(intnode_data) { // INSERT YOUR CODE HERE } }; void free_singly_linked_list(SinglyLinkedListNode* node) { // INSERT YOUR CODE HERE } // Complete the has_cycle function below. /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ bool has_cycle(SinglyLinkedListNode* head) { SinglyLinkedListNode* temp = head; bool isCycle = false; while (temp != nullptr) { // INSERT YOUR CODE HERE } } int main() { // INSERT YOUR CODE HERE TO TEST YOUR CODE return0; }arrow_forwardThe following code inserts an element at the beginning of the list public void insertBegin(T e) Node temp new Node (e); head temp; temp. Next = head; size++; True Falsearrow_forwarddata structures-java language quickly plsarrow_forward
- not sure how to do this problem output doesnt matterarrow_forwardWhen traversing a single- or double-linked list, you should be careful not to fall off the end of the list or you'll get a type your answer.arrow_forwardKWLinkedList<Integer> list = new KWLinkedList<Integer> (); for(int i = 1; i <= 4; i++) list.add( i ); ListIterator <Integer> iter = list.listIterator(); while(iter.hasNext()) { int x = iter.next(); int y = iter.next(); iter.set(x + y); } while(iter.hasPrevious()) System.out.print(iter.previous() + " ");arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education