home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 June / APC561.ISO / workshop / c / list.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-02  |  1011 b   |  76 lines

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct node_t {
  7.   struct node_t *next;
  8.   string data;
  9. };
  10.  
  11. class CStack {
  12. public:
  13.   CStack() { head=0; }
  14.   ~CStack() { Clear(); };
  15.   bool Push(const string &s);
  16.   bool Pop( string &s );
  17. private:
  18.   void Clear();
  19.   node_t *head;
  20. };
  21.  
  22. bool CStack::Push( const string &s )
  23. {
  24.   node_t *n = new node_t;
  25.   if (!n)
  26.     return false;
  27.   n->data = s;
  28.   n->next = head;
  29.   head = n;
  30.   return true;
  31. }
  32.  
  33. bool CStack::Pop( string &s )
  34. {
  35.   if (!head)
  36.     return false;
  37.   node_t *n = head;
  38.   s = n->data;
  39.   head = n->next;
  40.   delete n;
  41.   return true;
  42. }
  43.  
  44. void CStack::Clear()
  45. {
  46.   node_t *n = head;
  47.   
  48.   while (n) {
  49.     head = n->next;
  50.     delete n;
  51.     n = head;
  52.   }
  53.   head = 0;
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59.   string s;
  60.   CStack cs;
  61.   int count = 0;
  62.  
  63.   while (getline( cin, s ) && s.length()) {
  64.     count++;
  65.     if (!cs.Push( s ))
  66.       break;
  67.   }
  68.  
  69.   while (cs.Pop(s))
  70.     cout << s << '\n';
  71.  
  72.   return 0;
  73. }
  74.  
  75.  
  76.