zoukankan      html  css  js  c++  java
  • C++简单队列实现

    #ifndef SEQUEUE_H
    #define SEQUEUE_H
    
    #include<iostream>
    using namespace std;
    
    class SeQueue{
    public:
        SeQueue(int size=defaultMaxsize){
            if(size>0){
                maxsize=size;
                data=new int[maxsize];
                for(int i=0;i<maxsize;++i){
                    data[i]=0;
                }
                rear=front=-1;
            }
        }
        //判断队列是否为空
        bool Empty_SeQueue(SeQueue *s);
        //出队
        bool Pop_SeQueue(SeQueue *s,int *x);
        //入队
        int Push_SeQueue(SeQueue *s,int x);
    private:
        //默认长度
        const static int defaultMaxsize=10;
        //队列元素
        int *data;
        //最大长度
        int maxsize;
        //队首和队尾元素
        int rear,front;
    };
    
    bool SeQueue::Empty_SeQueue(SeQueue *s){
        if(s->rear==-1 && s->front==-1){
                return true;
            }else{
                return false;
            }
    }
    
    bool SeQueue::Pop_SeQueue(SeQueue *s,int *x){
        if(s->Empty_SeQueue(s)){
            return false;
        }
        *x=s->data[++s->front];
            return true;
    }
    
    int SeQueue::Push_SeQueue(SeQueue *s,int x){
        s->data[++s->rear]=x;
        return 1;
    }
    
    #endif
    #include<iostream>
    #include"SeQueue.h"
    using namespace std;
    
    int main(){
        int data[10]={7,1,2,3,4,5,6,7,8,9},a;
        int *p=&a;
        SeQueue *s=new SeQueue();
        for(int i=0;i<=5;++i){
            s->Push_SeQueue(s,data[i]);
        }
        if(s->Empty_SeQueue(s)){
            cout<<"入队失败"<<endl;
        }else{
            cout<<"入队成功"<<endl;
        }
        s->Pop_SeQueue(s,p);
        cout<<"出队的元素是:"<<*p<<endl;
        return 0;
    }
  • 相关阅读:
    Ajax三
    Ajax二
    【Verilog】组合逻辑写法
    【电路】LVDS 差分接口
    【C】数据类型定义
    【Flash】nv-ddr2接口Flash的ODT
    【vivado】clocking wizard 时钟配置
    【Linux】linux学习资料
    【Linux】ubuntu系统安装及软件依赖库
    【vivado】PL通过axi_hp接口控制PS的DDR
  • 原文地址:https://www.cnblogs.com/arno1394/p/11252286.html
Copyright © 2011-2022 走看看