// Chap 5, pp 210 - 211
// Includes WriteForward and WriteBackward2.
#include
#include
typedef struct nodeType* ptrType;
struct nodeType
{ char Data;
ptrType Next;
}; // end struct
void WriteForward(ptrType StringPtr)
// ---------------------------------------------------------
// Writes a string.
// Precondition: The string is represented as a linked list
// to which the pointer StringPtr points.
// Postcondition: The string is displayed. The linked list
// and StringPtr are unchanged.
// ---------------------------------------------------------
{
if (StringPtr != NULL)
{ // write the first character
cout << StringPtr->Data;
// write the string minus its first character
WriteForward(StringPtr->Next);
} // end if
} // end WriteForward
void WriteBackward2(ptrType StringPtr)
// ---------------------------------------------------------
// Writes a string backward.
// Precondition: The string is represented as a linked list
// to which the pointer StringPtr points.
// Postcondition: The string is displayed backward. The
// linked list and StringPtr are unchanged.
// ---------------------------------------------------------
{
if (StringPtr != NULL)
{ // write the string minus its first character backward
WriteBackward2(StringPtr->Next);
// write the first character
cout << StringPtr->Data;
} // end if
} // end WriteBackward2
// ******SAMPLE MAIN PROGRAM******
main()
{ const int StringLength = 80;
typedef char stringType[StringLength];
stringType InString;
ptrType StringPtr, Trav;
int Counter;
cout << "Enter a string: ";
cin >> InString;
// create a linked list that represents the character string
StringPtr = new nodeType;
StringPtr->Data = InString[0];
Trav = StringPtr;
for (Counter = 2; Counter <= strlen(InString); ++Counter)
{ Trav->Next = new nodeType;
Trav = Trav->Next;
Trav->Data = InString[Counter-1];
} // end for
Trav->Next = NULL;
// demonstrate WriteForward and WriteBackward2
cout << "\n\nThe string written forward is ===> ";
WriteForward(StringPtr);
cout << "\n\nThe string written backward is ==> ";
WriteBackward2(StringPtr);
cout << "\n\n";
return 0;
}
               (
geocities.com/siliconvalley/program/2864/ds)                   (
geocities.com/siliconvalley/program/2864)                   (
geocities.com/siliconvalley/program)                   (
geocities.com/siliconvalley)