zoukankan      html  css  js  c++  java
  • c++队列基本功能

    #include<string>
    #include<assert.h>
    #include<iostream>
    typedef int status;
    #define OK 1
    #define ERROR 0
    template<class type>
    class order_tream
    {

    public:
    order_tream(int a):size(a+1),n(0)
    {
    base =new type [a+1];
    assert(base!=0);
    front=0;
    rear=0;
    }
    int n;
    status full();//判断是否为满
    status empty();//判断是否为空
    void putin(int a);//输入
    status enqueue(type a);//进队列
    status sqqueue(type &a);//出队列
    void clear();//清空
    status out();//输出整个队列
    private:
    int rear;//尾
    int front;//头
    int size;
    type* base;
    };
    /*进队列*/
    template<class type>
    status order_tream<type>::enqueue(type a)
    {
    if( full())
    return ERROR;
    base[rear]=a;
    rear=(rear+1)%size;
    n++;
    return OK;
    }
    /*清空*/
    template<class type>
    void order_tream<type>::clear()
    {
    front=rear;
    }
    /*输入*/
    template<class type>
    void order_tream<type>::putin(int a)
    {
    type x;
    cout<<"输入开始"<<endl;
    for(int i=0;i<a;i++)
    {
    cin>>x;
    enqueue(x);
    }
    }
    /*出队列*/
    template<class type>
    status order_tream<type>::sqqueue(type& a)
    {
    if(empty())
    return ERROR;
    a=base[front];
    front=(front+1)%size;
    n--;
    return OK;
    }
    /*判断是否为空*/
    template<class type>
    status order_tream<type>::empty()
    {
    if(front==rear)
    return OK;
    else
    return ERROR;
    }
    /*判断是否为满*/
    template<class type>
    status order_tream<type>::full()
    {
    if(front==rear+1)
    return OK;
    else
    return ERROR;
    }
    /*输出整个队列*/
    template<class type>
    status order_tream<type>::out()
    {
    if(empty())
    return ERROR;
    type a;
    for(int i=0;i<n;i++)
    {
    a=base[i];
    cout<<a<<' ';
    }
    cout<<n;
    cout<<endl;
    return OK;
    }

  • 相关阅读:
    在Ubuntu1804上使用Apache2的部署Django配置
    UbuntuServer1804设置uwsgi自启动服务
    ubuntu 安装k8s 1.22.3 (VirtualBox虚拟机)
    启动keepalived 报错
    wasm-pack 编译错误 unexpected character 'u{0}'
    mariadb-安装
    K8S1.18 安装教程
    Ubuntu共享文件权限问题
    docker 安装consul
    Ubuntu 安装 MySQL 和远程连接
  • 原文地址:https://www.cnblogs.com/cyh1282656849/p/5543679.html
Copyright © 2011-2022 走看看