// File: Simple.cpp // Manual construction of an ordered linked list // - demonstrates four insertion cases // - list traversal for printing // - manual deletion. #include using namespace std; struct node { int data; node *next; }; void print(node *p) { while (p!=NULL) { cout << p -> data << ", "; p = p -> next; } } int main() { node *first; // Insert to empty list first = new node; // Create the node // Don't try these // *first.data = 10; // *(first.data) = 10; // This one works, but isn't optimal // (*first).data = 10; first -> data = 10; // -> operator in play // Insert at end first -> next = new node; first -> next -> data = 20; // Ground the list first -> next -> next = NULL; // Insert at head of list node *temp; temp=new node; temp -> data = 5; temp -> next = first; // Point new node at first first = temp; // Now it's safe to change first // Insert in middle temp = new node; temp -> data = 15; temp -> next = first -> next -> next; first -> next -> next = temp; print(first); temp = first -> next -> next; first -> next -> next = first -> next -> next -> next; delete temp; print(first); }