// illustrates linked list concepts with list of Strings // author : Owen Astrachan // 11/26/94 // reformatted by Eugene Wallingford, 03/06/97 #include #include "String.h" class StringNode { public: String info; StringNode * next; }; void Print(StringNode * list) // precondition: list is 0-terminated (last node's next field is 0) // postcondition: all info fields of list printed on one line { while (list != 0) { cout << list->info << " "; list = list->next; // move to next node } cout << endl; } void main () { StringNode a,b,c,d; // storage for 4 nodes a.info = "This"; a.next = &b; // initialize all nodes b.info = "is"; b.next = &c; c.info = "a"; c.next = &d; d.info = "test"; d.next = 0; Print(&a); StringNode e; // new node spliced into list e.info = "very long"; e.next = &d; c.next = &e; Print(&a); StringNode * ptr = &d; // "test" node a.next = ptr; // points at "test" node ptr->next = &b; // points at "is" node b.next = c.next; // skip the "a" node e.next = 0; // end sentence with "very long" Print(&a); ptr = new StringNode; ptr->info = "really"; ptr->next = &e; // "very long" node; b.next = ptr; // point at "really"node Print(&a); } /* ************* Output of the program ************ This is a test This is a very long test This test is very long This test is really very long ************************************************ */