用C++中的vector实现的栈

没事的时候随便写的,

模拟一下栈,

练习用C++写程序


 

 1#include<iostream>
 2
#include<vector>
 3
using namespace std;
 4  class Stack 
 5 {
 6 public:
 7    inline bool empty();
 8  bool full();
 9  bool push( int elem);
10  int pop();
11  void getmem();
12  int size(){return _stack.size();}
13
 private:
14  vector<int> _stack;
15 };
16  inline bool Stack::empty()
17 {
18          return _stack.empty();
19 }

20  
21 inline bool Stack::full()
22 {
23             return _stack.size()==_stack.max_size();
24 }

25
 bool Stack:: push(int elem)
26 {
27  if(full())
28   return false;
29  _stack.push_back(elem);
30  return true;
31 }

32    
33    int Stack:: pop( )
34 {
35  if(empty())
36   return false;
37  int elem=_stack.back();
38  _stack.pop_back();
39  return elem;
40 }

41
   void Stack::getmem()
42   {
43    if(empty())
44     return ;
45    cout<<_stack[0];
46    for(int ix=1;ix!=_stack.size();++ix)
47     cout<<' '<<_stack[ix];
48    cout<<endl;
49   }

50
int main()
51{
52  Stack s;
53  cout<<s.size()<<endl;
54  for(int i=0,j=1;i<1000;)
55  {
56   s.push(i+j);
57   int k=i;
58   i=j;
59   j=k+i;
60  }

61
  cout<<s.size()<<endl;
62  s.getmem();
63  s.pop();
64  s.pop();
65  s.getmem();
66
67 return 0;
68}

posted on 2010-03-28 19:03 田兵 阅读(787) 评论(0)  编辑 收藏 引用 所属分类: 算法笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年3月>
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿(2)

随笔分类(65)

随笔档案(65)

文章档案(2)

ACM

搜索

积分与排名

最新随笔

最新评论

阅读排行榜