最简单的栈

Stack.h

Code:
  1. #ifndef STACK_H   
  2. #define STACK_H   
  3. #include <string>   
  4. #include <vector>   
  5. using namespace std;   
  6. class Stack   
  7. {   
  8. public:   
  9.     Stack(string* lhs,string* rhs);   
  10.     bool push(const string& elem);   
  11.     bool pop(string& elem);   
  12.     bool peek(string& elem);   
  13.     bool full()const;   
  14.     int size();   
  15.     bool empty()const;   
  16.     void print()const;   
  17.     string& operator[](int index)   
  18.     {   
  19.         return _stack[index];   
  20.     }   
  21. private:   
  22.     vector<string> _stack;   
  23. };   
  24. #endif  

Stack.cpp

Code:
  1. #include <iostream>   
  2. #include "Stack.h"   
  3. Stack::Stack(string* lhs,string* rhs)   
  4. {   
  5.     while(lhs != rhs)   
  6.     {   
  7.         _stack.push_back(*lhs);   
  8.         ++lhs;   
  9.     }   
  10. }   
  11. bool Stack::empty() const  
  12. {   
  13.     return _stack.empty();   
  14. }   
  15. bool Stack::full()const  
  16. {   
  17.     return _stack.size() == _stack.max_size();   
  18. }   
  19. bool Stack::peek(string& elem)   
  20. {   
  21.     if (empty())   
  22.     {   
  23.         return false;   
  24.     }   
  25.     elem = _stack.back();   
  26.     return true;   
  27. }   
  28. bool Stack::push(const string& elem)   
  29. {   
  30.     if (full())   
  31.     {   
  32.         return false;   
  33.     }   
  34.     _stack.push_back(elem);   
  35.     return true;   
  36. }   
  37. bool Stack::pop(string &elem)   
  38. {   
  39.     if (empty())   
  40.     {   
  41.         return false;   
  42.     }   
  43.     elem = _stack.back();   
  44.     _stack.pop_back();   
  45.     return true;   
  46. }   
  47. int Stack::size()   
  48. {   
  49.     return _stack.size();   
  50. }   
  51. int main()   
  52. {   
  53.     string str[] = {"hello","world","I","Love","You!",":-)"};   
  54.     Stack stack(str,str+6);   
  55.     for (int i = 0; i < stack.size(); i++)   
  56.     {   
  57.         cout << stack[i] << " ";   
  58.     }   
  59.     cout << endl;   
  60.     string st;   
  61.     stack.peek(st);   
  62.     cout << "peek: " << st << endl;   
  63.     stack.pop(st);    
  64.     cout << "pop: " << st << endl;   
  65.     stack.push("MyLove");   
  66.     for (i = 0; i < stack.size(); i++)   
  67.     {   
  68.         cout << stack[i] << " ";   
  69.     }   
  70.     cout << endl;   
  71.     if (stack.empty())   
  72.     {   
  73.         cout << "no elem in stack!" << endl;   
  74.     }   
  75.     else  
  76.     {   
  77.         cout << "stack's elems are: " << endl;   
  78.           for (i = 0; i < stack.size(); i++)   
  79.           {   
  80.                 cout << stack[i] << " ";   
  81.           }   
  82.     cout << endl;   
  83.     }   
  84.     return 0;   
  85. }  

 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值