Question

C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is...

C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is already set.

Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed.

//////////////////////////////////////////////////////////////header.h//////////////////////////////////////////////////////////////////////////////////////////////

#ifndef HEADER_H_
#define HEADER_H_

#include
using namespace std;

template <class T>
class LL
{
private:
   struct LLnode
   {
       LLnode* fwdPtr;
       T theData;
   };
   LLnode* head;

public:
   LL();
   void push_front(T);
   void push_back(T);
   void destroy_list();
   int list_length();
};

template <class T>
LL<T> :: LL()
{
   head = nullptr;
}

template <class T>
void LL<T> :: push_front(T numb)
{
LLnode* ptr = new LLnode;
ptr -> theData = numb;
ptr -> fwdPtr = head;
head = ptr;
}

template <class T>
void LL<T> :: push_back(T numb)
{
LLnode* ptr = new LLnode;
ptr -> theData = numb;
ptr -> fwdPtr = nullptr;
if(head == nullptr)
{
head = ptr;
}
else
{
LLnode* temp = head;
while(temp -> fwdPtr != nullptr)
{
temp = temp-> fwdPtr;
}
temp -> fwdPtr = ptr;
}
}

template <class T>
void LL<T> :: destroy_list()
{
   while (head != nullptr)
   {
       LLnode* temp = head -> fwdPtr;
       delete head;
       head = temp;
   }
}

template <class T>
int LL<T> :: list_length()
{
int counter = 0;
LLnode* ptr = head;

while (ptr != nullptr)
{
counter++;
ptr = ptr->fwdPtr;
}
return counter;
}

#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////main.cpp//////////////////////////////////////////////////////////////////////////////////////

#include
using namespace std;
#include "header.h"

int main()

{

LL <string> ll1;
       cout << "main: length of empty list - " << ll1.list_length() << endl;

       cout << "main: trying to display initial size of ll1 - " << ll1.list_length() << endl;
       ll1.push_front("aaaaa");
       ll1.push_back("bbbbb");
       ll1.push_front("before aaaaa");
       ll1.push_back("after bbbbb");
       cout << "main: length of ll1 after 4 pushes - " << ll1.list_length() << endl;
       cout << "main: now trying to display ll1 after 4 push's" << endl;
       cout << "main: displaying final size of ll1 - " << ll1.list_length() << endl;
       ll1.destroy_list();
       cout << "main: displaying size of list 1 after destroy - " << ll1.list_length() << endl;
       cout << endl;

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* header.h */

#ifndef HEADER_H_
#define HEADER_H_

#include<iostream>
using namespace std;

template <class T>
class LL
{
private:
struct LLnode
{
LLnode* fwdPtr;
T theData;
};
LLnode* head;

public:
LL();
void push_front(T);
void push_back(T);
void destroy_list();
void recdestroy_list(LLnode*);
int list_length();
};

template <class T>
LL<T> :: LL()
{
head = nullptr;
}

template <class T>
void LL<T> :: push_front(T numb)
{
LLnode* ptr = new LLnode;
ptr -> theData = numb;
ptr -> fwdPtr = head;
head = ptr;
}

template <class T>
void LL<T> :: push_back(T numb)
{
LLnode* ptr = new LLnode;
ptr -> theData = numb;
ptr -> fwdPtr = nullptr;
if(head == nullptr)
{
head = ptr;
}
else
{
LLnode* temp = head;
while(temp -> fwdPtr != nullptr)
{
temp = temp-> fwdPtr;
}
temp -> fwdPtr = ptr;
}
}

/* Recursive Function to delete the entire linked list */
template <class T>
void LL<T> :: recdestroy_list(LLnode *head)
{
if (head){
   recdestroy_list(head->fwdPtr);
   delete head;   
   }   
}
template <class T>
void LL<T> :: destroy_list()
{
  
   recdestroy_list(head);
   head = NULL;
}

template <class T>
int LL<T> :: list_length()
{
int counter = 0;
LLnode* ptr = head;

while (ptr != nullptr)
{
counter++;
ptr = ptr->fwdPtr;
}
return counter;
}

#endif

/* main,cpp */

#include<iostream>
using namespace std;
#include "header.h"

int main()

{

LL <string> ll1;
cout << "main: length of empty list - " << ll1.list_length() << endl;

cout << "main: trying to display initial size of ll1 - " << ll1.list_length() << endl;
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "main: length of ll1 after 4 pushes - " << ll1.list_length() << endl;
cout << "main: now trying to display ll1 after 4 push's" << endl;
cout << "main: displaying final size of ll1 - " << ll1.list_length() << endl;
ll1.destroy_list();
cout << "main: displaying size of list 1 after destroy - " << ll1.list_length() << endl;
cout << endl;

return 0;
}

/* OUTPUT */

main: length of empty list - 0 main: trying to display initial size of 111 - 0 main: length of 111 after 4 pushes - 4 main: n

/* PLEASE UPVOTE */

Add a comment
Know the answer?
Add Answer to:
C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....

    You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

    Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty. A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function. For example, checkRecurseHelper() should return true for the linked list shown below. A main function (prob3.cpp) is given to you to add data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT