#include #include #include "vector.h" #include "CPstring.h" // file: stack.cc // author Dietolf Ramm; date: 12/8/96 // illustrate use of a linked list: a linked stack struct node { string info; node * next; }; void StackRead(node * & top) // precondition: top points to the top node of a stack or is null // postcondition: Lines read from file are pushed as nodes on the // stack { string line; node * ptr; ifstream datain("TagData"); while (getline(datain, line)) { cout << line << endl; ptr = new node; ptr -> next = top; ptr -> info = line; top = ptr; } } void StackOut(node * top) // precondition: top points to a stack // postcondition: info fields are written out from each node of // the stack. { while (top != 0) { cout << top -> info << endl; top = top -> next; } } int main() { node * top = 0; StackRead(top); cout << "From file in reverse order:" << endl; StackOut(top); return 0; } Sample output: stack 22301 Michael Ramm 22301 Nicholas Ramm 27278 Lenore Ramm 27701 Karl Ramm 22301 Barbara Ramm 37212 Eberhard Ramm 22301 Wolfhard Ramm 30582 Hartmut Ramm 27278 Mary-Kathlyn Ramm 27278 Dietolf Ramm 80802 Sieglind Ramm 37355 Ruth Ramm 27705 Dora Ramm 37355 Heinrich Ramm From file in reverse order: 37355 Heinrich Ramm 27705 Dora Ramm 37355 Ruth Ramm 80802 Sieglind Ramm 27278 Dietolf Ramm 27278 Mary-Kathlyn Ramm 30582 Hartmut Ramm 22301 Wolfhard Ramm 37212 Eberhard Ramm 22301 Barbara Ramm 27701 Karl Ramm 27278 Lenore Ramm 22301 Nicholas Ramm 22301 Michael Ramm