STL之顺序容器适配器(栈的链表实现代码)

STL中的顺序容器,还有顺序容器适配器:queu、priority_queue和stack。适配器是标准库中通用的概念,包括容器适配器、迭代器适配器和函数适配器。本质上适配器是使某一事物的行为类似另一事物的行为的一种机制。容器适配器让一种已存在的容器类型采用另一种不同的抽象类型的工作方式实现。

 

默认stack基于deque容器实现。但是stack适配器所关联的基础容器可以是任意一种顺序容器类型。

 

这份代码是用链表去实现!!!!

 

  1 #include<iostream>
  2 using namespace std;
  3 
  4 template <typename Object>
  5 class Stack
  6 {
  7 private:                                //私有嵌套结构体
  8     struct Node
  9     {
 10         Object data;
 11         Node *next;
 12 
 13         Node( const Object & d = Object( ), Node *n = NULL) : data( d ), next( n ) { }
 14     };
 15                                         /*与List里面的Node基本一致*/
 16 public:
 17     Stack( )
 18     {
 19         init( );
 20     }
 21                                         //构造函数,调用init函数,生成默认栈。
 22     ~Stack( )
 23     {
 24         clear( );
 25         delete  Top;
 26     }                                   //析构函数。
 27 
 28     /*Stack( const Stack & rhs)
 29     {
 30         init( )
 31         *this = rhs;
 32     }*/
 33 
 34     void push( const Object & x )
 35     {
 36         theSize++;
 37         Top = new Node( x, Top );
 38     }
 39                                         //压栈操作。
 40     const Object top( ) const
 41     {
 42         return Top->data;
 43     }
 44                                         //返回栈顶元素。
 45     int size( )
 46     {
 47         return theSize;
 48     }
 49                                         //返回栈中元素的多少。
 50     bool empty( )
 51     {
 52         return size( ) == 0;
 53     }
 54                                         //判断栈是否为空的!
 55     void pop( )
 56     {
 57         Top = Top->next;
 58         theSize--;
 59     }
 60                                         //删除栈顶元素的值。
 61     /*void clear( )
 62     {
 63         while( Top->next != NULL )
 64         pop( );
 65     }*/
 66 
 67 private:
 68     Node *Top;                          //指向栈顶指针。
 69     int theSize;                        //栈的大小。
 70 
 71     void init( )
 72     {
 73        theSize = 0;
 74        Top = new Node;
 75     }                                   //默认函数,调用将生成一个默认的栈,大小为空。
 76 };
 77 
 78 
 79 int main( )
 80 {
 81     Stack<int> s;
 82     cout << "输出默认生成的栈的大小:" << endl;
 83     cout << s.size( ) << endl;
 84     int num, m;
 85     cout << "输入需要压进栈的元素的数目:" << endl;
 86     cin >> num;
 87     for(int i = 1; i <= num; ++i)
 88     {
 89         cin >> m;
 90         s.push( m );
 91         cout << "栈顶的元素:" << endl;
 92         cout << s.top( ) << endl;
 93     }
 94     cout << "输出栈的大小:" << endl;
 95     cout << s.size( ) << endl;
 96     s.pop( );
 97     cout << "在pop操作之后,栈顶元素值为:" << endl;
 98     cout << s.top( ) << endl;
 99     cout << "在pop操作之后,栈的大小:" << endl;
100     cout << s.size( ) << endl;
101     cout << "栈的empty操作!" << endl;
102     if( s.empty( ) )
103     cout << "It is empty!" << endl;
104     else
105     cout << "It is not empty!" << endl;
106     return 0;
107 }
posted @ 2012-09-16 14:11  alan_forever  阅读(854)  评论(0编辑  收藏  举报