1. /* 
  2.  * sqstack.cpp 
  3.  * 
  4.  *  Created on: 2012-5-12 
  5.  *      Author: awind 
  6.  */ 
  7.  
  8. #include <iostream> 
  9. #include<memory.h>
  10. #define STACK_INIT_SIZE 100     //存储空间初始分配量 
  11. #define STACKINCREMENT  10      //存储空间分配增量 
  12. using namespace  std; 
  13.  
  14. typedef struct 
  15.     int *base; 
  16.     int *top; 
  17.     int stackSize; 
  18. }SqStack; 
  19.  
  20. bool InitStack( SqStack &s ) 
  21.     s.base = new int[STACK_INIT_SIZE]; 
  22.     if( !s.base )    //如果该栈非空,,表示存储失败,则将其置为空 ? 
  23.         return false
  24.     s.top = s.base; 
  25.     s.stackSize = STACK_INIT_SIZE; 
  26.     return true
  27. bool GetTop( SqStack s, int &e ) 
  28.     if( s.top == s.base ) 
  29.     { 
  30.         cout<<"the stack is empty!"<<endl; 
  31.         return false
  32.     } 
  33.     e = *( s.top - 1 ); 
  34.     return true
  35. bool Push( SqStack &s, int e ) 
  36.     if( s.top - s.base >= s.stackSize )  //栈满,则追加存储空间 
  37.     { 
  38.         int *pNewBase = new int[ ( s.stackSize + STACKINCREMENT ) ]; 
  39.         memcpy( pNewBase, s.base, sizeofint ) * s.stackSize );//1为搬迁位置,2为搬迁前位置, 
  40.                                                                 //3为搬迁的数量大小。 
  41.         delete []s.base;   //删除原来的存储空间 
  42.         s.base = pNewBase;   //将栈地和栈顶重新赋新值 
  43.         s.top = s.base + s.stackSize;  //栈顶指向增加的第一块空间 
  44.         s.stackSize += STACKINCREMENT; 
  45.     } 
  46.     *s.top++ = e; 
  47.     return true
  48. bool Pop( SqStack &s, int &e ) 
  49.     if( s.top == s.base ) 
  50.         return false
  51.     e = *--s.top; 
  52.     return true
  53. bool StackEmpty( SqStack s ) 
  54.     if( s.top == s.base ) 
  55.         return true
  56.     else 
  57.         return false
  58. int main() 
  59.     int n,e; 
  60.     SqStack stack; 
  61.     InitStack(stack); 
  62.     cout<<"input the num you want to push: "
  63.  
  64.     while(cin>>n) 
  65.     { 
  66.         Push(stack,n); 
  67.     } 
  68.  
  69.     while( !StackEmpty( stack ) ) 
  70.     { 
  71.         Pop( stack, e ); 
  72.         cout<<e<<endl; 
  73.     } 
  74.  
STL中vector实现:
 stack.h   
 
  
  1. /* 
  2.  * stack.h 
  3.  * 
  4.  *  Created on: 2012-5-12 
  5.  *      Author: awind 
  6.  */ 
  7.  
  8. #ifndef STACKV_H_ 
  9. #define STACKV_H_ 
  10.  
  11. #include <iostream> 
  12. #include <cstdlib> 
  13. #include <vector> 
  14.  
  15. #define STACK_INIT_SIZE 10 
  16.  
  17. using namespace std; 
  18.  
  19. class Stack 
  20. public
  21.     Stack(); 
  22.     bool push(int element); 
  23.     bool isEmpty() const
  24.     int pop(); 
  25.     bool isfull(); 
  26.     void getmem(); 
  27.     int size(); 
  28.  
  29.  
  30. private
  31.     vector<int> stack; 
  32. }; 
  33.  
  34.  
  35.  
  36. #endif /* STACKV_H_ */ 


stack.cpp

 

 
  
  1. /* 
  2.  * stack.cpp 
  3.  * 
  4.  *  Created on: 2012-5-12 
  5.  *      Author: awind 
  6.  */ 
  7.  
  8. #include "stackv.h" 
  9.  
  10. using namespace std; 
  11.  
  12.  
  13. Stack::Stack() 
  14.     stack.reserve(STACK_INIT_SIZE); //初始化设置栈的容量 
  15.  
  16. bool Stack::isEmpty() const 
  17.     return stack.empty(); 
  18.  
  19.  
  20. bool Stack::isfull() 
  21.     return stack.size()==stack.max_size(); 
  22.  
  23.  
  24. bool Stack::push(int element) 
  25.     if(isfull()){  //栈满则增加栈的容量 
  26.         stack.reserve(stack.size()+STACK_INIT_SIZE); 
  27.     } 
  28.     stack.push_back(element); 
  29.     return true
  30.  
  31.  
  32.  
  33. int Stack::pop() 
  34.     if(isEmpty()) 
  35.         return false
  36.     int element=stack.back(); 
  37.     stack.pop_back(); 
  38.     return element; 
  39.  
  40.  
  41. void Stack::getmem() 
  42.     if(isEmpty()) 
  43.         return
  44.  
  45.     for(size_t i=0;i!=stack.size();++i) 
  46.     { 
  47.         cout<<stack[i]<<" "
  48.     } 
  49.     cout<<endl; 
  50.  
  51. int Stack::size() 
  52.     return stack.size(); 
  53.  
  54. int main() 
  55.     Stack s; 
  56.     cout<<s.size()<<endl; 
  57.     for(int i=0;i<10;i++) 
  58.     { 
  59.         s.push(i); 
  60.  
  61.     } 
  62.  
  63.     cout<<s.size()<<endl; 
  64.  
  65.  
  66.     s.getmem(); 
  67.  
  68.     return 0;