/* * Example showing how pointers can be used to create a simple linked list. */ #include using namespace std; struct ListNode { int data; ListNode* next; }; int main() { ListNode* head = NULL; int k; // reads in integers from input until a non-integer is encountered, // ignoring white space (spaces, tabs, newlines) while (cin >> k) { ListNode* newNode = new ListNode; // create and newNode->data = k; // initialize a new list node newNode->next = head; // link the new node in to the list head = newNode; // update the head pointer } // print out the list // note that it is in reverse order for (ListNode* curr = head; curr != NULL; curr = curr->next) { cout << curr->data << ' '; cout << endl; } return 0; }