#include
#include //for abs
#include //for find, distance, count, min_element, max_element
#include //for accumulate
#include "node.h"
using namespace std;
int main()
{
//Construct a list containing 10, 15, 30.
node *head = new node(30, 0);
head = new node(15, head); //Insert 15 ahead of 30.
head = new node(10, head); //Insert 10 ahead of 15.
for (const node *p = head; p != 0; p = p->next) {
cout<< p->value << "\n";
}
cout << "\n";
const node::iterator begin(head);
const node::iterator end;
//for (node::iterator it = begin; operator!=(it, end); it.operator++()) {
for (node::iterator it = begin; it != end; ++it) {
cout << *it << "\n"; //cout << it.operator*() << "\n";
}
cout<< "\n";
node::iterator it = begin;
++it; //it.operator++();
*it = 20; //it.operator*() = 20; overwrite the 15.
const node::iterator found = find(begin, end, 20);
if (found == end) { //if (operator==(found, end)) {
cout << "20 was not found.\n";
} else {
cout << "20 is at position " << distance(begin, found) << ".\n";
}
cout << "Value 20 occurs " << count(begin, end, 20) << " times.\n"
<< "There are " << distance(begin, end) << " values.\n"
<< "Sum of the values is " << accumulate(begin, end, 0) << ".\n"
<< "Product of the values is "
<< accumulate(begin, end, 1, multiplies())
<< ".\n";
if (begin != end) {
cout << "Smallest value is " << *min_element(begin, end)
<<".\n";
const node::iterator biggest = max_element(begin, end);
cout << "Biggest value is " << *biggest << ", at position "
<< distance(begin, biggest) << ".\n";
}
for (const node *p = head; p != 0;) {
const node *const prev = p;
p = p->next;
delete prev; //can do this even though prev is a const *
}
return EXIT_SUCCESS;
}