{================================================================== liststrings.p Started by Jeff Ondich on 5/23/96 Last modified 11/6/96 This program demonstrates the use of linked lists to implement a string data type. ==================================================================} program liststrings(input,output); type nodePtr = ^node; node = record data : char; next : nodePtr end; var message : nodePtr; {================================================================= ReadString reads characters from the keyboard until reaching the end of a line. The characters are put into a linked list, with str pointing to the head of the list (which is also the first character read). =================================================================} procedure ReadString( var str : nodePtr ); var currentNode : nodePtr; begin if eoln then str := nil else begin new( currentNode ); read( currentNode^.data ); str := currentNode; while not eoln do begin new( currentNode^.next ); currentNode := currentNode^.next; read( currentNode^.data ) end; currentNode^.next := nil end end; {================================================================== WriteString prints a string stored as a linked list of characters. ==================================================================} procedure WriteString( str : nodePtr ); var currentNodePtr : nodePtr; begin currentNodePtr := str; while currentNodePtr <> nil do begin write( currentNodePtr^.data ); currentNodePtr := currentNodePtr^.next end end; {================================================================== WriteBackwards prints in reverse order a string stored as a linked list of characters. ==================================================================} procedure WriteBackwards( str : nodePtr ); begin if str <> nil then begin WriteBackwards( str^.next ); write( str^.data ) end end; {================================================================== main program ==================================================================} begin write( 'Type something: ' ); ReadString( message ); write( 'You typed: ' ); WriteString( message ); writeln; write( 'Here it is, backwards: ' ); WriteBackwards( message ); writeln end.