// SimpleLinkedList.C // Burton Rosenberg // 3 September 1997 // The facts of life, according to Linked Lists. #include struct DataBox { char item ; DataBox * next ; } ; int main(int argc, char * argv[]) { // Get a line of input const int BUFFER_SIZE = 1000 ; char buffer[BUFFER_SIZE] ; cout << "Enter a line of text: " ; cin.getline(buffer, BUFFER_SIZE) ; // Make the dummy header node DataBox * myLinkedList = new DataBox ; myLinkedList->next = NULL ; myLinkedList->item = '\0' ; // ASSERT (Postcondition): myLinkedList is an empty linked list // Add all char's from buffer to myLinkedList char * p = buffer ; DataBox * l = myLinkedList ; // LOOP INVARIANT: l points to last DataBox on myLinkedList // and p points to next character to process while( *p!='\0' ) { // for each character, build a box and fill it l->next = new DataBox ; l = l->next ; l->item = *p ; l->next = NULL ; p++ ; // ASSERT: l points to last DataBox on myLinkedList // and p points to next character to process } // Print the linked list. l = myLinkedList->next ; // skip over header // LOOP INVARIANT: l points to next item to print while ( l!=NULL ) { cout << l->item ; l = l->next ; // ASSERT: l points to next item to print, or is NULL } cout << endl ; }