Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 17, Problem 35RQE
Program Plan Intro
Purpose of the given code:
The given code is trying to destroy the members of a linked list by using a destructor. A destructor is called when the program ends or the destructor function calls.
Given Code:
//Definition of destructor
NumberList::~NumberList()//Line 1
{//Line 2
//Declaration of structure pointer variables
ListNode *nodePtr, *nextNode;//Line 3
//Storing "head" pointer into "nodePtr"
nodePtr = head;//Line 4
//loop
while (nodePtr != nullptr)//Line 5
{//Line 6
//Assign address of next into "nextNode"
nextNode = nodePtr->next;//Line 7
//Error
nodePtr->next=nullptr;//Line 8
//Assign nextNode into "nodePtr"
nodePtr = nextNode;//Line 9
}//Line 10
}//Line 11
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
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;
}
None
in 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 =…
Chapter 17 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Ch. 17.1 - Prob. 17.1CPCh. 17.1 - Prob. 17.2CPCh. 17.1 - Prob. 17.3CPCh. 17.1 - Prob. 17.4CPCh. 17.2 - Prob. 17.5CPCh. 17.2 - Prob. 17.6CPCh. 17.2 - Prob. 17.7CPCh. 17.2 - Prob. 17.8CPCh. 17.2 - Prob. 17.9CPCh. 17.2 - Prob. 17.10CP
Ch. 17 - Prob. 1RQECh. 17 - Prob. 2RQECh. 17 - Prob. 3RQECh. 17 - Prob. 4RQECh. 17 - Prob. 5RQECh. 17 - Prob. 6RQECh. 17 - Prob. 7RQECh. 17 - Prob. 8RQECh. 17 - Prob. 9RQECh. 17 - Prob. 10RQECh. 17 - Prob. 11RQECh. 17 - Prob. 13RQECh. 17 - Prob. 14RQECh. 17 - Prob. 15RQECh. 17 - Prob. 16RQECh. 17 - Prob. 17RQECh. 17 - Prob. 18RQECh. 17 - Prob. 19RQECh. 17 - Prob. 20RQECh. 17 - Prob. 21RQECh. 17 - Prob. 22RQECh. 17 - Prob. 23RQECh. 17 - Prob. 24RQECh. 17 - Prob. 25RQECh. 17 - T F The programmer must know in advance how many...Ch. 17 - T F It is not necessary for each node in a linked...Ch. 17 - Prob. 28RQECh. 17 - Prob. 29RQECh. 17 - Prob. 30RQECh. 17 - Prob. 31RQECh. 17 - Prob. 32RQECh. 17 - Prob. 33RQECh. 17 - Prob. 34RQECh. 17 - Prob. 35RQECh. 17 - Prob. 1PCCh. 17 - Prob. 2PCCh. 17 - Prob. 3PCCh. 17 - Prob. 4PCCh. 17 - Prob. 5PCCh. 17 - Prob. 6PCCh. 17 - Prob. 7PCCh. 17 - List Template Create a list class template based...Ch. 17 - Prob. 9PCCh. 17 - Prob. 10PCCh. 17 - Prob. 11PCCh. 17 - Prob. 12PCCh. 17 - Prob. 13PCCh. 17 - Prob. 14PCCh. 17 - Prob. 15PC
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
- data structures in javaarrow_forwarddata structures-java language quickly plsarrow_forward//main.cpp #include "linkedListType.h"int main(){Node* head = NULL;orderedLinkedList oll = orderedLinkedList(head); oll.insert(40);oll.insert(100);oll.insert(50); oll.printList(); oll.insert(50);oll.printList(); return 0;} //linkedListType.h #include <stdio.h>#include <iostream>using namespace std; class Node{public:int data;Node* next;}; class orderedLinkedList {Node* head; public:orderedLinkedList(Node* h) {head = h;} void insert(int new_data){Node* head_ref = head; while (head_ref != NULL && (head_ref->next != NULL) && (head_ref->next)->data < new_data){cout << head_ref->data << "\n";head_ref = head_ref->next;} if (head_ref != NULL && (head_ref->next != NULL) && (head_ref->next)->data == new_data){cout << "ERROR: Item to be inserted is already in the list\n";return;} /* Insert new node */Node* new_node = new Node();new_node->data = new_data;if (head_ref != NULL) {new_node->next =…arrow_forward
- Given 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_forwardprivate static final List<Polynomial> polyList = new ArrayList<>(); //main here public static void main(String[] args) { processPolyList(); } //=============================================================================================== // method: fromFile / returns: Arraylist // description: Opens a file takes a String and tokenizes it into an ArrayList of of expressions //=============================================================================================== public static ArrayList<String> farrow_forwardMultiple 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 itemarrow_forward
- The definition of linked list is given as follows: struct Node { ElementType Element ; struct Node *Next ; } ; typedef struct Node *PtrToNode, *List, *Position; If L is head pointer of a linked list, then the data type of L should be ??arrow_forward6. Suppose that we have defined a singly linked list class that contains a list of unique integers in ascending order. Create a method that merges the integers into a new list. Note the additional requirements listed below. Notes: ● . Neither this list nor other list should change. The input lists will contain id's in sorted order. However, they may contain duplicate values. For example, other list might contain id's . You should not create duplicate id's in the list. Important: this list may contain duplicate id's, and other list may also contain duplicate id's. You must ensure that the resulting list does not contain duplicates, even if the input lists do contain duplicates.arrow_forwardstruct 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.arrow_forward
- starter code: in java pls and thank you! public class LinkedList { private Node head; private Node tail; public void add(String item) { Node newItem = new Node(item); // handles the case where the new item // is the only thing in the list if (head == null) { head = newItem; tail = newItem; return; } tail.next = newItem; tail = newItem; } public void print() { Node current = head; while (current != null) { System.out.println(current.item); current = current.next; } } public void printWithSkips() { // TODO your code here } class Node { String item; Node next; public Node(String item) { this.item = item; this.next = null; } } } and public class Driver { public static void…arrow_forwardC++ program LinkList: namespace { template<typename T> class List { public: virtual ~List(){}; virtual bool addFirst(T) = 0; virtual bool addLast(T) = 0; virtual T& get(int) = 0; virtual bool insert(int, T) = 0; virtual T remove(int) = 0; virtual T removeFirst() = 0; virtual T removeLast() = 0; virtual void set(int, T) = 0; virtual int size() const = 0; }; } #endif //PROG1_LIST_Harrow_forwardclass Node: def __init__(self, e, n): self.element = e self.next = n class LinkedList: def __init__(self, a): # Design the constructor based on data type of a. If 'a' is built in python list then # Creates a linked list using the values from the given array. head will refer # to the Node that contains the element from a[0] # Else Sets the value of head. head will refer # to the given LinkedList # Hint: Use the type() function to determine the data type of a self.head = None # To Do # Count the number of nodes in the list def countNode(self): # To Do # Print elements in the list def printList(self): # To Do # returns the reference of the Node at the given index. For invalid index return None. def nodeAt(self, idx): # To Doarrow_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