• 栈 和 队列


    class Q{//队列类
      private:
        int head;
        int tail;
        int length;
        bool flag;
        int *p;
      public:
        Q(int c,int a = 0,int b = 0,int d = 0){//构造函数
          head = a;
          tail = b;
          length = c;
          flag = d;
          p = new int[c];
          for(int i = 0;i < length;i++){
            p[i] = 0;
          }
        }
        void Qprint(){
          for(int j = 0;j <length;j++){
            cout<<p[j]<<endl;
          }
        }
        void ENQ(int x){//入队列
          if(flag == 1){
            if(tail == length){
              cout<<tail;
              tail = 0;
              if(tail == head){
                cout<<"上溢出!"<<head<<endl;
                tail = length;
              }else{
                p[length-1] = x;
              }
            }else{
              tail = tail+1;
              if(tail == head){
                cout<<"上溢出!"<<endl;
                tail = tail-1;
              }else{
                p[tail-1] = x;
              }
            }
          }else{
            tail = tail+1;
            p[head] = x;
            flag = 1;
          }
        }
        int DEQ(){//出队列
          if(flag == 1){
            if(head != tail){
              if(head < length-1){
                head++;
                return p[head-1];
              }else{
                head = 0;
                return p[length-1];
              }
            }else{
              return p[head];
              flag = 0;
            }
          }else{
            cout<<"下溢出!"<<endl;
            return -1;
          }
        }
    };
    class S{//栈类
      private:
        int head;
        int tail;
        int length;
        bool flag;
        int *p;
      public:
        S(int c,int a = 0,int b = 0,int d = 0){//构造函数
          head = a;
          tail = b;
          length = c;
          flag = d;
          p = new int[c];
          for(int i = 0;i < length;i++){
            p[i] = 0;
          }
        }
        void Sprint(){
          for(int j = 0;j <length;j++){
            cout<<p[j]<<endl;
          }
        }
        void PUSH(int x){//入栈
          if(flag == 1){
            if(tail == length){
              cout<<tail;
              tail = 0;
              if(tail == head){
                cout<<"上溢出!"<<head<<endl;
                tail = length;
              }else{
                p[length-1] = x;
              }
            }else{
              tail = tail+1;
              if(tail == head){
                cout<<"上溢出!"<<endl;
                tail = tail-1;
              }else{
                p[tail-1] = x;
              }
            }
          }else{
            tail = tail+1;
            p[head] = x;
            flag = 1;
          }
       }
        int POP(){//出栈
          int temp = tail;
          if(head != tail){
            if(tail != 0){
              tail--;
            }else{
              tail = length-1;
            }
            return p[tail];
          }else{
            cout<<"下溢出!"<<endl;
            tail = temp;
            return -1;
          }
        }
    };
    测试函数:
    void main(){
      int num[] = {8,2,3,9,9,5,6,8,1,4,6,7,5};
      Q a(13); 
      for(int k = 0;k < 13;k++) a.ENQ(num[k]); 
      for(int b = 0;b < 13;b++) cout<<a.DEQ()<<endl;
      S m(13); 
       for(int i = 0;i < 13;i++)m.PUSH(num[i]); 
       for(int j = 0;j < 13;j++)cout<<m.POP()<<endl;
    }
  • 相关阅读:
    八数码问题及其扩展
    java注释
    康托展开和逆康托展开
    模线性方程组
    欧拉函数
    扩展欧几里德
    商城项目实战 | 2.2 Android 仿京东商城——自定义 Toolbar (二)
    商城项目实战 | 2.1 Android 仿京东商城——自定义 Toolbar (一)
    商城项目实战 | 1.1 Android 仿京东商城底部布局的选择效果 —— Selector 选择器的实现
    Android使用Path实现仿最新淘宝轮播广告底部弧形有锯齿的问题以及解决办法
  • 原文地址:https://www.cnblogs.com/candycloud/p/3341518.html
Copyright © 2020-2023  润新知