>how do I get the last one to point to the one before it
Your constructor does the work for you:
Code:
#include <iostream>
class node {
public:
node(int init, node *link);
node *next_link();
int get_item();
private:
int item;
node *next;
};
node::node(
int init,
node *link
)
: item(init)
, next(link)
{
// No body
}
node *
node::next_link()
{
return next;
}
int
node::get_item()
{
return item;
}
int
main()
{
node *head = 0;
for (int i = 0; i < 10; i++) {
head = new node(i, head);
}
for (node *it = head; it; it = it->next_link()) {
std::cout<< it->get_item() <<std::endl;
}
}
>how do I index through to print them out (like I do with the array)?
See the last loop of the above code. That's the usual idiom for traversing a singly linked list.