// C++ Edit this program calculate the average of the nodes in the list (CalcAvg
ID: 3696468 • Letter: #
Question
// C++
Edit this program
calculate the average of the nodes in the list (CalcAvg )
append a node to the end of the list (EndNode)
pre-pend a node to the front of the list (FirstNode)
give the position of a node return its value (ValueNode)
Run the program creating the folllowing nodes
(0, 7.0), (1, 12.0) , (2, 16.5), (3, 1.4)
add a node of your choice to the end and begining
search for what value is located in node 0
display the list after each change
#include <iostream>
using namespace std;
class Node {
public:
double data;
Node* next;
};
class List{
Node* head;
public:
List(void){ head = NULL; }
~List(void);
bool Isempty() { return head == NULL; }
Node* InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
//Private:
//Node* head;
};
int main(void)
{
//int num;
//cout<<"how many data :";
//cin >>num;
List list;
list.InsertNode(0, 7.0); //successful
//------------------>";
list.DisplayList();
list.InsertNode(1, 12.0); // successful
list.DisplayList();
list.InsertNode(2, 16.5); // successful
list.DisplayList();
list.InsertNode(3, 1.4); // successful // print all the elements
list.DisplayList();
list.InsertNode(-1, 5.0);
list.DisplayList();
if (list.FindNode(5) > 0) cout << "5 found" << endl;
else
cout << "5 not found" << endl;
if (list.FindNode(4) > 0) cout << "4 found" << endl;
else
cout << "4 not found" << endl;
list.DeleteNode(7);
list.DisplayList();
//system("pause");
cin.get();
return 0;
}
Node* List::InsertNode(int index, double x) {
int currIndex = 0;
Node* currNode = head;
Node* prevNode = NULL;
while (currNode && x > currNode->data) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
Node* newNode = new Node;
newNode->data = x;
if (currIndex == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = prevNode->next;
prevNode->next = newNode;
}
return newNode;
}
int List::FindNode(double x){
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode)
return currIndex;
else
return 0;
}
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
cout << currNode->data << "--->";
currNode = currNode->next;
num++;
}
cout << " NULL" << endl << endl;
cout << "Number of nodes in the list: " << num << endl;
}
List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}
Explanation / Answer
#include <iostream>
using namespace std;
class Node {
public:
double data;
Node* next;
};
class List{
Node* head;
public:
List(void){ head = NULL; }
~List(void);
bool Isempty() { return head == NULL; }
Node* InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
double CalcAvg();
Node* EndNode(double x);
Node* FirstNode(double x);
double ValueNode(int position);
int size();
//Private:
//Node* head;
};
int main(void)
{
//int num;
//cout<<"how many data :";
//cin >>num;
List list;
list.InsertNode(0, 7.0); //successful
list.DisplayList();
list.InsertNode(1, 12.0); // successful
list.DisplayList();
list.InsertNode(2, 16.5); // successful
list.DisplayList();
list.InsertNode(3, 1.4); // successful // print all the elements
list.DisplayList();
list.InsertNode(-1, 5.0);
list.DisplayList();
list.FirstNode(2.5);
list.EndNode(3.5);
list.DisplayList();
// list.CalcAvg();
// list.ValueNode(0);
// list.ValueNode(1);
// list.ValueNode(2);
// list.ValueNode(3);
// list.ValueNode(4);
// list.ValueNode(5);
// list.ValueNode(6);
// if (list.FindNode(5) > 0) cout << "5 found" << endl;
// else
// cout << "5 not found" << endl;
// if (list.FindNode(4) > 0) cout << "4 found" << endl;
// else
// cout << "4 not found" << endl;
list.DeleteNode(7);
list.DisplayList();
//system("pause");
cin.get();
return 0;
}
Node* List::InsertNode(int index, double x) {
if(index>=0){
int currIndex = 0;
Node* currNode = head;
Node* prevNode = NULL;
while (currIndex!=index) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
Node* newNode = new Node;
newNode->data = x;
if (currIndex == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = prevNode->next;
prevNode->next = newNode;
}
return newNode;
} else {
cout<<"Give position greater than 0"<<endl;
return NULL;
}
}
int List::FindNode(double x){
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode)
return currIndex;
else
return 0;
}
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
cout << currNode->data << "--->";
currNode = currNode->next;
num++;
}
cout << " NULL" << endl << endl;
cout << "Number of nodes in the list: " << num << endl;
}
int List::size()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
currNode = currNode->next;
num++;
}
return num;
}
List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}
double List::CalcAvg()
{
double sum = 0;
int num=0;
Node* currNode = head;
while (currNode != NULL){
// cout << currNode->data << "--->";
sum += currNode->data;
currNode = currNode->next;
num++;
}
double avg = sum/num;
// cout << " NULL" << endl << endl;
cout << "Average of nodes in the list: " << avg << endl;
}
double List::ValueNode(int position){
if(position>=1){
Node* currNode = head;
int currIndex = 1;
while (currIndex != position) {
currNode = currNode->next;
currIndex++;
}
if (currNode){
cout<<"Node value at "<<position<<" position is"<<currNode->data<<endl;
return currNode->data;
}
else {
cout<<"Node is not found"<<endl;
return 0;
}
} else {
cout<<"Node not found: "<<endl;
}
}
Node* List::EndNode(double x) {
int endPosition = size();
return InsertNode(endPosition,x);
}
Node* List::FirstNode(double x) {
return InsertNode(0,x);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.