以下是引用片段: #include <fstream> #include <iostream> #include <string> using namespace std; class Stack ...{ struct Link ...{ Link* next; void* data; Link(void* dat, Link* nxt) : data(dat) ,next(nxt) ...{} }*head; public : Stack() : head(0) ...{} void push(void* data) ...{ head = new Link(data, head); } void* pop() ...{ if (head == 0) return 0; void* object = head->data; Link* oldHead = head; head = oldHead->next; delete oldHead; return object; } void* peek() ...{ return head ? head->data : 0; } }; int main() ...{ ifstream in("Stack.cpp"); Stack text; string line; while(getline(in, line)) text.push(new string(line)); string* s; while((s = (string*)text.pop()) != 0) ...{ cout << *s << endl; delete s; } } |