in cpp please   AnyList.h #ifndef ANYLIST_H #define ANYLIST_H #include #include        //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;    //constructor AnyList::AnyList() {    ptrToFirst = nullptr;    count = 0; }    //insertFront void AnyList::insertFront(int newData) {      //Node *ptrToNewNode = new Node;   //create a pointer named ptrToNewNode that                                    // points to an unnamed node    //ptrToNewNode->setData(newData);       //store data in the new node    //ptrToNewNode->setPtrToNext(ptrToFirst);   //set new node's pointer to point to the first node    //ptrToFirst = ptrToNewNode;       //make the new node be the "first" node    //    //   There is shorter way to write the statements above using the overloaded constructor: /*           Node *ptrToNewNode = new Node(newData, ptrToFirst);            ptrToFirst = ptrToNewNode;   */      //   Or even better, without creating any pointers:    //              ptrToFirst = new Node(newData, ptrToFirst);    //*/      ++count; }    //print void AnyList::print() const {    if (ptrToFirst == nullptr) // check if the list is empty                            // you can also use: if (count < 1)        cerr << "List is empty."; // use cerr, rather than cout (why?)    else    {        Node *current = ptrToFirst; //create a pointer to traverse the list                                      //this pointer will start with the first node in the list        while (current != nullptr)   //while the current pointer is NOT pointing to NULL        {                       // that is, the current pointer has not reached the            // end of the list            cout << current->getData() << " ";   //output the data            current = current->getPtrToNext();       //move the pointer current forward        }    } }    //destroyList - Does not delete the list object; it ONLY deletes the nodes. void AnyList::destroyList() { Node *temp = ptrToFirst; //pointer to delete the node, starting with the first node       while (ptrToFirst != nullptr) {        ptrToFirst = ptrToFirst->getPtrToNext(); delete temp;        temp = ptrToFirst; }    count = 0; }    //destructor AnyList::~AnyList() {    destroyList(); } main.cpp #include "AnyList.h" #include using namespace std; int main() {    AnyList myList;    myList.insertFront(2);    myList.insertFront(3);    myList.insertFront(4);    myList.insertFront(5);    myList.insertFront(6);    cout << "Inserted: 2 3 4 5 6\n";    cout << "List is: ";    myList.print();    cout << endl;       myList.destroyList(); // the list object still exists,                        // but the list is empty.    cout << "\nPrint the list after emptying it...";    cout << "\nList is: ";    myList.print();    cout << endl;    cout << endl;    system("Pause");    return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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;

   //constructor
AnyList::AnyList()
{
   ptrToFirst = nullptr;
   count = 0;
}

   //insertFront
void AnyList::insertFront(int newData)
{  
   //Node *ptrToNewNode = new Node;   //create a pointer named ptrToNewNode that
                                   // points to an unnamed node

   //ptrToNewNode->setData(newData);       //store data in the new node

   //ptrToNewNode->setPtrToNext(ptrToFirst);   //set new node's pointer to point to the first node

   //ptrToFirst = ptrToNewNode;       //make the new node be the "first" node

   //
   //   There is shorter way to write the statements above using the overloaded constructor:

/*           Node *ptrToNewNode = new Node(newData, ptrToFirst);
           ptrToFirst = ptrToNewNode;   */  

   //   Or even better, without creating any pointers:
   //  
           ptrToFirst = new Node(newData, ptrToFirst);
   //*/  

   ++count;
}

   //print
void AnyList::print() const
{
   if (ptrToFirst == nullptr) // check if the list is empty
                           // you can also use: if (count < 1)
       cerr << "List is empty."; // use cerr, rather than cout (why?)
   else
   {
       Node *current = ptrToFirst; //create a pointer to traverse the list  
                                   //this pointer will start with the first node in the list

       while (current != nullptr)   //while the current pointer is NOT pointing to NULL
       {                       // that is, the current pointer has not reached the
           // end of the list
           cout << current->getData() << " ";   //output the data
           current = current->getPtrToNext();       //move the pointer current forward
       }
   }
}

   //destroyList - Does not delete the list object; it ONLY deletes the nodes.
void AnyList::destroyList()
{
Node *temp = ptrToFirst; //pointer to delete the node, starting with the first node
  
   while (ptrToFirst != nullptr)
{
       ptrToFirst = ptrToFirst->getPtrToNext();
delete temp;
       temp = ptrToFirst;
}

   count = 0;
}

   //destructor
AnyList::~AnyList()
{
   destroyList();
}

main.cpp

#include "AnyList.h"

#include <iostream>
using namespace std;

int main()
{
   AnyList myList;

   myList.insertFront(2);
   myList.insertFront(3);
   myList.insertFront(4);
   myList.insertFront(5);
   myList.insertFront(6);

   cout << "Inserted: 2 3 4 5 6\n";
   cout << "List is: ";
   myList.print();
   cout << endl;
  
   myList.destroyList(); // the list object still exists,
                       // but the list is empty.

   cout << "\nPrint the list after emptying it...";
   cout << "\nList is: ";
   myList.print();
   cout << endl;

   cout << endl;
   system("Pause");
   return 0;
}

For this assignment you are to turn the AnyList class that you have already done into a template class called SLL.
The data will be the templated type.
You will create a new templated Node class in its own .h and .cpp
• Private member variables - m_data and m_next
• Default Constructor with data default constructed.
Overloaded Constructor - sets data and next.
Accessors and Mutators ( set and get )
Overload the operator<< to print the data (no newline)
You will create a new templated class called SLL which is a singly linked list using the
templated Node in its own .h and .cpp
• Private member variables - m_first and m_count
• Default Constructor
Copy Constructor
Overloaded assignment operator
Destructor
• Method push to add an element to the front of the list.
• Method pop which removes and returns the element at the front.
Method find which returns true if an element is in the list.
• Overload the operator<< to print the list with spaces between the elements.
Create a main method to fully test your SLL class with different data types.
Transcribed Image Text:For this assignment you are to turn the AnyList class that you have already done into a template class called SLL. The data will be the templated type. You will create a new templated Node class in its own .h and .cpp • Private member variables - m_data and m_next • Default Constructor with data default constructed. Overloaded Constructor - sets data and next. Accessors and Mutators ( set and get ) Overload the operator<< to print the data (no newline) You will create a new templated class called SLL which is a singly linked list using the templated Node in its own .h and .cpp • Private member variables - m_first and m_count • Default Constructor Copy Constructor Overloaded assignment operator Destructor • Method push to add an element to the front of the list. • Method pop which removes and returns the element at the front. Method find which returns true if an element is in the list. • Overload the operator<< to print the list with spaces between the elements. Create a main method to fully test your SLL class with different data types.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Operations of Linked List
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
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education