【C++封装循环顺序队列和顺序栈】

C++封装循环顺序队列和顺序栈

手动封装一个循环顺序队列类(Stack)
私有成员属性:存放队列的数组、两个变量分别记录队头和队尾下标公有成员函数:入队(push( type value ))
出队(pop())
展示(show)
求队列长度(size())︰要求时间复杂度在常量级别判满( bool full())
判空(bool empty())

循环顺序队列的运行结果和代码如下

在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/d0a08780b2244907a45bd30049d6ac2a.png

#include <iostream>

using namespace std;

int max;

class Stack{    //类
private:
    int *data;     //数据域
    int front;     //头指针
    int tail;      //尾指针

public:
    void init(int size)         //初始化队列
    {
        data = new int[size];        //申请对应队列长度的空间
        front = 0;                   //头
        tail = 0;                    //尾
        ::max = size;
    }

    bool empty()              //判空
    {
        return front == tail;
    }

    bool full()                //判满
    {
        return (tail+1)%(::max) == front;
    }

    void push(int data)           //进队列
    {
        if(full())
        {
            cout<< "队列已满,入队失败"<< endl;
            return;
        }
        this->data[tail] = data;
        tail = (tail+1)%(::max);
    }

    void pop()                  //出队列
    {
        if(empty())
        {
            cout<< "队列为空,出队失败"<< endl;
            return;
        }
        int data;                         //存放数据域的数据
        data = this->data[front];         //this指针
        this->data[front] = 0;            //this指针
        front = (front+1) % (::max+1);    //寻找队头
    }

    void mysize()                    //队列的长度
    {
        int count = (::max-front+tail) % ::max;
        cout<< "循环顺序队列的长度为:"<< count<< endl;
    }

    void show()                       //展示
    {
        if(empty())
        {
            cout<< "队列为空,遍历失败"<< endl;
            return;
        }
        for(int i = front; i != tail; i = (i+1)%(::max))
        {
            cout<< (this->data[i])<< endl;
        }
    }
    void destroy()       //销毁申请空间
    {
        delete []data;
    }
};

int main()
{
    int a = 0;
    Stack s1;        //类起别名为s1
    cout<< "请输入队列的大小:"<< endl;
    cin>> a;
    s1.init(a);             //初始化队列长度

    s1.show();              //遍历显示队列内容
    cout<<"*********************************"<<endl;
    cout<<endl;

    s1.push(1);         //入队
    s1.push(2);         //入队
    s1.push(3);         //入队
    s1.push(4);         //入队
    s1.push(5);         //入队
    s1.push(6);         //入队
    s1.push(7);         //入队
    s1.show();          //遍历显示队列内容
    cout<<"*********************************"<<endl;
    cout<<endl;

    s1.push(8);         //入队
    s1.show();          //遍历显示队列内容
    cout<<"*********************************"<<endl;
    cout<<endl;

    s1.mysize();
    s1.destroy();
}

顺序栈的运行结果和代码如下

在这里插入图片描述

#include <iostream>
#define N 5                    //顺序栈长5
using namespace std;

class My_stack{
private:
  int *data;                 //数据域
  int top;                   //栈顶

public:
  void create()              //创建顺序栈
  {
      data = new int[N];       ///申请对应大小的空间
      top = -1;                //栈顶初始化
  }

  bool full()                 //判顺序栈满
  {
      return top == N-1;
  }

  bool empty()                //判顺序栈空
  {
      return top == -1;
  }

  int push(int data)         //入顺序栈
  {
      if(full())
      {
          cout<< "栈已满,入栈失败"<<endl;
          return -1;
      }
      this->data[++top] = data;
      return 0;
  }

  int pop()                  //出顺序栈
  {
      if(empty())
     {
          cout<<"栈为空,出栈失败"<<endl;
          return -1;
      }
      top--;
      return 0;
  }

  void traverse()                //遍历顺序栈
  {
      if(empty())
      {
          cout<< "顺序栈为空,遍历失败"<< __LINE__<< endl;
          return;
      }
      for(int i = top; i >= 0; i--)
      {
          cout<<" "<<this->data[i]<<endl;
      }
  }

  void destroy()       //销毁申请空间
  {
      delete []data;
  }

};

int main()
{
    My_stack s1;              //初始化类
    s1.create();              //创建
    s1.traverse();             //遍历
    cout<<"*******************************************"<<endl;
    cout<< endl;

    s1.push(10);        //入顺序栈
    s1.push(20);        //入顺序栈
    s1.push(30);        //入顺序栈
    s1.push(40);        //入顺序栈
    s1.push(50);        //入顺序栈
    s1.traverse();      //遍历
    cout<<"*******************************************"<<endl;
    cout<< endl;

    s1.push(60);        //入顺序栈
    s1.traverse();      //遍历
    cout<<"*******************************************"<<endl;
    cout<< endl;

    s1.pop();       //出栈
    s1.traverse();

    s1.destroy();       //释放
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值