#include <iostream> // basic i/o
using namespace std;

struct Node{
	int value;
	char label;
	Node* next;
};

int nodeSum(Node* current){
	int total = 0;
	while (current->next != nullptr){
		cout << current->label << ": " << current->value << endl;
		total += current->value;
		current = current->next;
	}
	cout << current->label << ": " << current->value << endl;
	total += current->value;
	cout << "total: ";
	return total;
}

float nodeAvg(Node* current){
	int total = 0;
	int nodeCount = 0;
	float average;
	while (current->next != nullptr){
		cout << current->label << ": " << current->value << endl;
		total += current->value;
		nodeCount++;
		current = current->next;
	}
	cout << current->label << ": " << current->value << endl;
	total += current->value;
	cout << "total: " << total << endl;
	cout << "number of nodes: " << nodeCount << endl;
	if(nodeCount > 0){
		average = total/nodeCount
		return average;
	}else{
		cout << "ERROR: No nodes: divide by zero.";
	}
	return 0;
}

int main(){
	Node a,b,c;
	Node d,e,f;
	Node* first = nullptr;

	a.value = 1;
	a.label = 'a';
	b.value = -1;
	b.label = 'b';
	c.value = 2;
	c.label = 'c';

	a.next = &b;
	b.next = &c;
	c.next = nullptr;

	first = &a;

	cout << "Node a:\naddress: " << &a << ", value: " << a.value << ", next address: " << a.next << endl;
	cout << "Node b:\naddress: " << &b << ", value: " << b.value << ", next address: " << b.next << endl;
	cout << "Node c:\naddress: " << &c << ", value: " << c.value << ", next address: " << c.next << endl;

	cout << "Sum of the first List (a,b,c):\n" << nodeSum(first) << endl;
	cout << "Average of the first list (a,b,c):\n" << nodeAvg(first) << endl;

	d.value = 1;
	d.label = 'd';
	e.value = 0;
	e.label = 'e';
	f.value = 2;
	f.label = 'f';
	
	first = &f;

	f.next = &e;
	e.next = &d;
	d.next = nullptr;

	cout << "Node d:\naddress: " << &d << ", value: " << d.value << ", next address: " << d.next << endl;
	cout << "Node e:\naddress: " << &e << ", value: " << e.value << ", next address: " << e.next << endl;
	cout << "Node f:\naddress: " << &f << ", value: " << f.value << ", next address: " << f.next << endl;

	cout << "Sum of the second List (f,e,d):\n" << nodeSum(first) << endl;
	cout << "Average of the second list (f,e,d):\n" << nodeAvg(first) << endl;

	// How about linking the two separate lists?
	c.next = &f;
	cout << "Node c:\naddress: " << &c << ", value: " << c.value << ", next address: " << c.next << endl;
	first = &a;
	cout << "Sum of the combined List (a,b,c,f,e,d):\n" << nodeSum(first) << endl;
	cout << "Average of the second list (a,b,c,f,e,d):\n" << nodeAvg(first) << endl;
	
	return 0;
}