Question
In C++. you will be writing a couple of functions. A circular linked list is like a normal linked list, except that the last node
In C++. you will be writing a couple of functions. A circular linked list is like a normal linked list, except that the last node in the list points back to the first node in the list instead of containing a null pointer. For this lab, you will need to write the following four functions in clist.cpp, and add function prototypes for them to clist.h. File supplied.o contains code that can build, display, duplicate, and destroy a circular linked list.
int count(node * head) Iteratively compute and return the number of nodes in the circular linked list.
int countR(node * head) Recursively compute and return the number of nodes in the circular linked list.
int sum(node * head) Iteratively compute and return the sum of the ints contained in the circular linked list.
int sumR(node * head) Recursively compute and return the sum of the ints contained in the circular linked list.
----------
Given:
---------------
app.cpp
#include
#include "clist.h"
using namespace std;
#pragma GCC diagnostic ignored "-Wunused-variable"
int main()
{
node* head{nullptr};
/* Builds a circular linked list with a random number of nodes
*containing randomly-chosen numbers.
*/
build(head);
display(head);
// PUT YOUR CODE HERE to call the functions assigned,
// and print out the results. For example,
//
// cout << "iterative sum: " << sum(head) << endl;
//
// The code for your functions should be in clist.cpp.
// When called the 2nd time, this also prints the total
// of the numbers in the nodes.
display(head);
int nNodesFreed{0};
node* n{head};
node* temp;
while( n != head || ! nNodesFreed) {
temp = n->next;
delete n;
n = temp;
nNodesFreed++;
}
cout << "# nodes freed: " << nNodesFreed << endl;
//destroy(head);
return 0;
}
---------------------
clist.h
//clist.h
#include
#include
#include
struct node
{
int data;
node* next;
};
// These functions are in supplied.o and can be called from your code.
void build(node*& head);
void display(node* head);
void destroy(node* &head);
void duplicate(node*& new_copy); //Provides a duplicate copy of the list.
// Add the prototypes for your functions below this comment.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started