//created by Dr. Ramsey //linked list beginnings #include using namespace std; //you may use a class with everything public or use a struct: struct node { int value; node *next; }; node * addtofront (node *head, int x); //you could use a reference to a pointer here //void addtofront (node *&head, int x); void print(node *head); int main() { node *head=NULL; int i = 0; while( i == 0) { //print the list cout << "list: "; print(head); //get a value from the user int x; cout <<"enter a value: "; cin >> x; //go ahead and add to the front of the list head = addtofront(head,x); //or if you used the pointer reference: //addtofront(head,x); //print the list cout << "newlist: "; print(head); //see if the user wishes to continue cout << "0: more 1: quit: "; cin >> i; } } node * addtofront(node *head, int x) { node *curr = new node; curr->value = x; curr->next = head; return curr; //this is the new head of the list } /* //the pointer reference method void addtofront(node *&head, int x) { node *curr = new node; curr->value = x; curr->next = head; head = curr; } */ void print(node *head) { //print something for an empty list if(head == NULL) { cout << "Empty List" << endl; return; } //print the head node *curr = head; cout << curr->value; curr = curr->next; //move past the head and print the rest //I do it this way to set up the commas while(curr != NULL) { cout << ", " << curr->value; curr = curr->next; } cout << endl; }