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; }
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images