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 1FTE
Explanation of Solution
Purpose of the given code:
The given code is used to display the second element in a list “myList”. This list contains three elements.
Given code:
/* Set a reference "ref" to the first node in the list "myList" */
Node ref = myList; //Line 1
/* Increment the value of "ref" */
ref++;//Line 2
/* Display the element in the "ref" */
System.out.print(ref.value); //Line 3
Error in the given code:
- The error is present in “line 2” of given code. If user needs move on to the next node, then they should set a reference to the successor of the existing node. That is assign “ref” to “ref.next”.
- Therefore, user should replace “ref = ref.next” in the place of “ref++”...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
struct nodeType {
int infoData;
nodeType * next;
};
nodeType *first;
… and containing the values(see image)
Using a loop to reach the end of the list, write a code segment that deletes all the nodes in the list. Ensure the code performs all memory ‘cleanup’ functions.
None
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
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
- Duplicate Set This function will receive a list of elements with duplicate elements. It should add all of the duplicate elements to a set and return the set containing the duplicate elements. A duplicate element is an element found more than one time in the specified list. The order of the set does not matter. Signature: public static HashSet<Object> duplicateSet(ArrayList<Object> list) Example: INPUT: [2, 4, 5, 3, 3, 5] OUTPUT: {5, 3}arrow_forwardT/F: All Linked Lists must have head node.arrow_forwardstruct remove_from_front_of_dll { // Function takes no parameters, removes the book at the front of a doubly // linked list, and returns nothing. void operator()(const Book& unused) { //// TO-DO (13) |||| // Write the lines of code to remove the book at the front of "my_dll", // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. ///// END-TO-DO (13) //// } std::list& my_dll; };arrow_forward
- struct node{ int a; struct node * nextptr; }; Write two functions. One for inserting new values to a link list that uses the given node structure. void insert(struct node **head, int value); Second function is called to count the number of even numbers in the link list. It returns an integer that represents the number of even numbers. int countEvenNumbers(struct node *head); Write a C program that reads a number of integers from the user and insert those integers into a link list (use insert function). Later pass the head pointer of this link list to a function called countEvenNumbers. This function counts and returns the number of even numbers in the list. The returned value will be printed on the screen. Note 1: Do not modify the function prototypes. Sample Input1: Sample Output1: 45 23 44 12 37 98 33 35 -1 3 Sample Input2: Sample Output2: 11 33 44 21 22 99 123 122 124 77 -1 4arrow_forwardIt is a to the next node in the linked list, which is the property after it in the Node class.arrow_forwardOption+Explanationarrow_forward
- Ocaml Mode Write a function mode : ’a list -> ’a = that given a (nonempty) list it returns an element that repeats the most times (if ties exist, return the element among them that appears first in the list). examples mode [1;2;1;4;2;5;1;2;6];; - : int = 1 mode [’a’;’b’;’c’;’b’;’a’;’b’;’d’;’c’;’d’];; - : char = ’b’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_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_forwardcard_t * moveCardBack (card t *head); The moveCardBack function will take the card in front of the pile and place it in the back. In coding terms, you are taking the head of the linked list and moving it to the end. The function has one parameter which is the head of the linked list. After moving the card to the back, the function returns the new head of the linked list.arrow_forwardin cpp please AnyList.h #ifndef ANYLIST_H#define ANYLIST_H #include<iostream>#include <string> //Need to include for NULL class Node{public: Node() : data(0), ptrToNext(nullptr) {} Node(int theData, Node *newPtrToNext) : data(theData), ptrToNext(newPtrToNext){} Node* getPtrToNext() const { return ptrToNext; } int getData( ) const { return data; }void setData(int theData) { data = theData; } void setPtrToNext(Node *newPtrToNext) { ptrToNext = newPtrToNext; } ~Node(){}private:int data; Node *ptrToNext; //pointer that points to next node}; class AnyList{public: AnyList(); void insertFront(int); void print() const; void destroyList(); ~AnyList(); private: Node *ptrToFirst; //pointer to point to the first node in the list int count; //keeps track of number of nodes in the list}; #endif AnyList.cpp #include "AnyList.h" using namespace std; //constructorAnyList::AnyList(){ ptrToFirst = nullptr; count =…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