Emmma W.

asked • 08/07/21

slove the problem in c++

#include<iostream>
using namespace std;

class Node
{
public:
int value;
Node *next;
};

class LinkedList
{
private:
// head always points to the first node
// If there is no node, then head is NULL
Node *head;
public:
LinkedList()
{
head=NULL;
}

// Adds a node containing the passed
// value at the end of the list
void appendNode(int val)
{
Node *n = new Node;
n->value = val;
n->next = NULL;

if(head==NULL)
{
head=n;
}
else
{
Node *p = head;
while(p->next!=NULL)
{
p=p->next;
}
p->next = n;
}
}

void deleteAllNodes()
{
deleteAllNodesRecursive(head);
}

void deleteAllNodesRecursive(Node *n)
{
// complete this function
}

void printList()
{
printListRecursive(head);
}

void printListRecursive(Node *n)
{
// complete this function
}

int countNodes()
{
return countNodesRecursive(head);
}

int countNodesRecursive(Node *n)
{
// complete this function
return -1;
}
};

int main()
{
// DO NOT CHANGE THE MAIN FUNCTION

LinkedList L;
L.appendNode(3);
L.appendNode(5);
L.appendNode(7);
L.appendNode(2);

L.printList(); // Should print 3->5->7->2->
cout << L.countNodes() << endl; // Should print 4
L.deleteAllNodes();
L.printList(); // Should print nothing
}


1 Expert Answer

By:

Patrick B. answered • 08/08/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.