>>My problem is when I insert stuff into the linked list and then print it out the output I recieve is backwards.
What you're doing is adding at the front of the list, what you want is to add at the end. The easiest way is to maintain a tail node
Code:
#include <iostream>
using namespace std;
struct node {
int item;
node *next;
node(int init, node *link): item(init), next(link){}
};
int main()
{
node *head, *tail;
node *np, *forward;
head = new node(0, 0); // Dummy head
tail = head;
for (int i = 1; i < 6; i++)
{
tail->next = new node(i, 0);
tail = tail->next;
}
cout<<"Head -- "<< head->item <<endl;
cout<<"Tail -- "<< tail->item <<endl;
for (np = head->next; np != 0; np = forward)
{
forward = np->next;
cout<< np->item <<flush;
delete np;
}
}