//---------------------------------------------------------------------------- // // FILE : palindrome.C // AUTHOR : Eugene Wallingford // CREATION DATE : October 29, 1997 // //---------------------------------------------------------------------------- // // MODIFIED BY : // DATE : // DESCRIPTION : // //---------------------------------------------------------------------------- #include #include "list.h" // header for List -- ListElementType is char #include "stack.h" // header for Stack -- StackElementType is char int palindrome ( List & ); int display_list( List & ); int main() { char word_as_string[21]; cout << "Enter a string of up to 20 characters: "; cin >> word_as_string; List l; for (int i = 0; word_as_string[i] != '\0'; i++) l.insert(word_as_string[i]); if ( palindrome( l ) ) cout << "Yes! This word is a palindrome: "; else cout << "No, sorry, but this word is not a palindrome: "; display_list( l ); } int palindrome ( List & word ) { Stack s; StackElementType ch; // build the stack if ( word.first( ch ) ) { s.push( ch ); while ( word.next( ch ) ) s.push( ch ); } // compare forwards and backwards word.first( ch ); while ( !s.isEmpty() ) { if ( s.pop() != ch ) return 0; word.next( ch ); } return 1; } int display_list( List & l ) { ListElementType elem; bool notEmpty(l.first(elem)); while (notEmpty) { cout << elem; notEmpty = l.next(elem); } cout << endl << endl; }