1)栈

  1 #include<iostream>
  2 #include<iomanip>
  3 using namespace std;
  4 
  5 enum error{overflow,underflow,success};
  6 const int maxlen=100;
  7 
  8 class  stack
  9 {
 10 public:
 11      stack();//初始化
 12      ~stack();//析构
 13     bool empty() const;//判断空
 14     bool full() const;//判断满
 15     int get_front(int &x)const;//取栈顶元素
 16     error push(const int x);//入栈
 17     error pop();//出栈
 18 private:
 19     int count;//统计栈中元素的个数
 20     int data[maxlen];//存储栈中数据
 21 };
 22 
 23 /*
 24 *初始化栈
 25 */
 26  stack:: stack()
 27 {
 28     count=0;
 29 }
 30  
 31  /*
 32  *判断为空
 33  */
 34 bool stack::empty() const{
 35     if(count==0)return true;
 36     return false;
 37 }
 38 
 39 /*
 40 *判断为满
 41 */
 42 bool stack::full() const{
 43     if(count==maxlen)return true;
 44     return false;
 45 }
 46 
 47 /*
 48 *取栈顶元素
 49 */
 50 int stack::get_front(int &x)const{
 51     if(empty())return underflow;
 52     x=data[count-1];
 53     return success;
 54 }
 55 
 56 /*
 57 *入栈
 58 */
 59 error stack::push(const int x){
 60     if(full())return overflow;
 61     data[count]=x;
 62     count++;
 63     return success;
 64 }
 65 
 66 /*
 67 *出栈
 68 */
 69 error stack::pop(){
 70     if(empty())return underflow;
 71     count--;
 72     return success;
 73 }
 74 stack::~stack()
 75 {
 76     while(!empty())pop();
 77 }
 78 
 79 /*
 80 *十进制数转化为八进制数
 81 */
 82 int xchg(int n,stack s){
 83     cout<<"十进制:["<<n<<"]->8进制:";
 84     int mod,x;
 85     while(n!=0){
 86         mod = n % 8;
 87         s.push(mod);
 88         n/=8;
 89     }
 90     while(s.empty()!=true){
 91         s.get_front(x);
 92         cout<<x;
 93         s.pop();
 94     }
 95     cout<<endl;
 96     return 0;
 97 }
 98 int main()
 99 {
100     stack s;
101     xchg(100,s);
102     return 0;
103 }
104  
原文地址:https://www.cnblogs.com/minmsy/p/5021885.html