#include #include #define NIL (0) class ListNode { public: char data; ListNode *next; ListNode(char d) { data = d; next = NIL; } }; class Stack { ListNode *_top; ListNode *_tail; public: Stack() { _top = NIL; _tail = NIL; } void insert(char data) { if (_top == NIL) { _top = new ListNode(data); _tail = _top; } else if (_top->next == NIL) { _top->next = new ListNode(data); } else if (_top->next->next == NIL) { ListNode *p = _top->next; _top->next = new ListNode(data); _top->next = p; _top = _top->next; } else { _top->next = new ListNode(data); _tail = _top; } } char print() { if (_tail == NIL) return NIL; char c = _tail->data; putchar(c); _tail = _tail->next; return c; } }; void main() { Stack aStack; char *s = strdup("9214757"); for (int i = 0; i < strlen(s); i++) { aStack.insert(s[i]); } while (aStack.print() != NIL); putchar('\n'); }