NEED HELP FILLING THE METHODS THE CPP file #include using namespace::std; struct
ID: 669010 • Letter: N
Question
NEED HELP FILLING THE METHODS THE CPP file
#include
using namespace::std;
struct node
{
int data;
node * p;
};
int length(const char *p)
{
int ans = 0;
for (int i = 0; p[i]; i++){
ans++;
}
return ans;
}
int lengthR(const char *p)
{
if (*p == '') return 0;
return 1 + lengthR(p + 1);
}
int lengthR(const node * st)
{
if (st == NULL) return 0;
return 1 + lengthR(st->p);
}
int length(const node * st)
{
const node * walker = st;
if (st == NULL) return 0;
return walker + (length(st->);
}
int sum(const int *, int howMany)
{
}
int sumR(const int *, int howMany)
{
}
int sum(const node *p)
{
}
int sumR(const node *p)
{
}
int max(const char *ptr)
{
}
int maxR(const node *ptr)
{
}
void makeLinkedList(node * & start)
{
}
void printLinkedList(ostream &, const node *)
{
}
void deleteLinkedList(node *)
{
}
int length( const char *);
int lengthR(const char *);
int length( const node *);
int lengthR(const node *);
int sum(const int *, int howMany);
int sumR(const int *, int howMany);
int sum(const node *);
int sumR(const node *);
int sum(const char *); // sum ASCII values
int sumR(const char *); // sum ASCII value
int max(const char *);
int maxR(const char *);
int max(const int * , int howMany);
int maxR(const int *, int howMany);
int max(const node *);
int maxR(const node *);
void makeLinkedList(node * & start);
void printLinkedList(ostream &, const node *);
void deleteLinkedList(node *);
void main()
{
cout << length("abcdefgh") << endl;
cout<< lengthR("abcd");
node * st;
makeLinkedList(st);
cout << st->p->p->data << endl;
cout << lengthR(st) << endl;
printLinkedList(cout,st);
deleteLinkedList(st);
}
void makeLinkedList(node * & start)
{
// make the linked list 7 -> 12 -> 6 -> 15 -> 3
start = new node;
start->data = 7;
start->p = new node;
start->p->data = 12;
start->p->p = new node;
start->p->p->data= 6;
start->p->p->p = new node;
start->p->p->p->data= 15;
start->p->p->p->p= new node;
start->p->p->p->p->data= 3;
start->p->p->p->p->p= NULL;
}
void printLinkedList(ostream & out, const node * s)
{
const node * walker = s;
while ( walker != NULL)
{
out << walker->data << " ";
walker = walker->p;
}
out << endl;
}
void deleteLinkedList(node *s)
{
if ( s == NULL ) return; // 0 nodes
if ( s->p == NULL ) // 1 node
{
delete s;
return;
}
node * prev = s;
node * current = prev->p;
while ( current != NULL )
{
cout << "deleting " << prev-> data << endl;
delete prev;
prev = current;
current = current->p;
}
cout << "deleting " << prev-> data << endl;
delete prev;
cout << "decestructor leaving " << endl;
}
Explanation / Answer
CODE :
#include <iostream>
using namespace std;
struct node
{
int data;
node * p;
};
int length(const char *p)
{
int ans = 0;
for (int i = 0; p[i]; i++){
ans++;
}
return ans;
}
int lengthR(const char *p)
{
if (*p == '') return 0;
return 1 + lengthR(p + 1);
}
int lengthR(const node * st)
{
if (st == NULL) return 0;
return 1 + lengthR(st->p);
}
int length(const node * st)
{
const node * walker = st;
if (st == NULL) return 0;
return walker + (length(st->);
}
int sum(const int *, int howMany)
{
int total;
for(int i=0; i < howMany; i++)
total += *howMany;
return total;
}
int sumR(const int *, int howMany)
{
if (* howMany == '') return 0;
return 1 + sumR(howMany + 1);
}
int sum(const node *p)
{
const node * listOfNumbers = p;
if (p == NULL) return 0;
return listOfNumbers + (sum(p->);
}
int sumR(const node *p)
{
if (p == NULL) return 0;
return 1 + sumR(p->howMany);
}
int max(const char *ptr)
{
int tr = 0;
for (int i = 0; i < ptr; i++){
tr++;
}
return tr;
}
int maxR(const node *ptr)
{
if (ptr == NULL) return 0;
return maxR(ptr->p);
}
void makeLinkedList(node * & start)
{
start = new node;
start->data = 7;
start->p = new node;
start->p->data = 12;
start->p->p = new node;
start->p->p->data= 6;
start->p->p->p = new node;
start->p->p->p->data= 15;
start->p->p->p->p= new node;
start->p->p->p->p->data= 3;
start->p->p->p->p->p= NULL;
}
void printLinkedList(ostream &, const node * s)
{
const node * walker = s;
while ( walker != NULL)
{
cout << walker->data << " ";
walker = walker->p;
}
cout << endl;
}
void deleteLinkedList(node *s)
{
if ( s == NULL ) return 0;
if ( s->p == NULL )
{
delete s;
return 0;
}
node * prev = s;
node * current = prev->p;
while ( current != NULL )
{
cout << "deleting " << prev-> data << endl;
delete prev;
prev = current;
current = current->p;
}
cout << "deleting " << prev-> data << endl;
delete prev;
cout << "destructor leaving " << endl;
}
int length( const char *);
int lengthR(const char *);
int length( const node *);
int lengthR(const node *);
int sum(const int *, int howMany);
int sumR(const int *, int howMany);
int sum(const node *);
int sumR(const node *);
int sum(const char *); // sum ASCII values
int sumR(const char *); // sum ASCII value
int max(const char *);
int maxR(const char *);
int max(const int * , int howMany);
int maxR(const int *, int howMany);
int max(const node *);
int maxR(const node *);
void makeLinkedList(node * & start);
void printLinkedList(ostream &, const node *);
void deleteLinkedList(node *);
void main()
{
cout << length("abcdefgh") << endl;
cout<< lengthR("abcd");
node * st;
makeLinkedList(st);
cout << st->p->p->data << endl;
cout << lengthR(st) << endl;
printLinkedList(cout,st);
deleteLinkedList(st);
}
void makeLinkedList(node * & start)
{
// make the linked list 7 -> 12 -> 6 -> 15 -> 3
start = new node;
start->data = 7;
start->p = new node;
start->p->data = 12;
start->p->p = new node;
start->p->p->data= 6;
start->p->p->p = new node;
start->p->p->p->data= 15;
start->p->p->p->p= new node;
start->p->p->p->p->data= 3;
start->p->p->p->p->p= NULL;
}
void printLinkedList(ostream & out, const node * s)
{
const node * walker = s;
while ( walker != NULL)
{
out << walker->data << " ";
walker = walker->p;
}
out << endl;
}
void deleteLinkedList(node *s)
{
if ( s == NULL ) return; // 0 nodes
if ( s->p == NULL ) // 1 node
{
delete s;
return;
}
node * prev = s;
node * current = prev->p;
while ( current != NULL )
{
cout << "deleting " << prev-> data << endl;
delete prev;
prev = current;
current = current->p;
}
cout << "deleting " << prev-> data << endl;
delete prev;
cout << "decestructor leaving " << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.