23062C++&QTday3

1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量

成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

头文件stack.c

cpp 复制代码
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <cstring>

using namespace std;
class stack
{
private:
    int data[128];
    int top;
public:
    //构造函数
    stack();
    //析构函数
    ~stack();
    //拷贝构造函数
    stack(const stack &other);
    //入栈
    int stack_push(int e);
    //出栈
    int stack_pop();
    //清空栈
    void stack_clear();
    //判空
    bool stack_empty();
    //判满
    bool stack_full();
    //获取栈顶元素
    int &stack_top();
    //求栈的大小
    int stack_size();
};
#endif // STACK_H

源文件:

cpp 复制代码
#include"stack.h"
//构造函数
stack::stack()
{
    cout<<"构造函数"<<endl;
}
//析构函数
stack::~stack()
{
    delete[] data;
    cout<<"析构函数"<<endl;
}
//拷贝构造函数
stack::stack(const stack &other)
{
   
    cout<<"拷贝构造函数"<<endl;
}
//入栈
int stack::stack_push(int e)
{
    top++;
    data[top]=e;
    cout<<"入栈成功"<<endl;
    return 0;
}
//出栈
int stack::stack_pop()
{
    top--;
    cout<<"出栈成功"<<endl;
    return 0;
}
//清空栈
void stack::stack_clear()
{
    top=-1;
    return;
}
//判空
bool stack::stack_empty()
{
    return top==-1;
}
//判满
bool stack::stack_full()
{
    return top==127;
}
//获取栈顶元素
int & stack::stack_top()
{
    return data[top];
}
//求栈的大小
int stack::stack_size()
{
    return top+1;
}

测试文件:

cpp 复制代码
#include"stack.h"

int main()
{
    stack s1;
    s1.stack_push(1);
    s1.stack_push(2);
    s1.stack_pop();
    cout << "s1.data[top]="<<s1.stack_top()<< endl;
    return 0;
}

运行结果:

2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置

成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

头文件:

cpp 复制代码
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;

class queue
{
private:
  int data[128];
  int front=0;
  int tail=0;
public:
  //构造函数
  queue();
  //析构函数
  ~queue();
  //拷贝构造函数
  queue(const queue &other );
  //入队
  int queue_push(int e);
  //出队
  int queue_pop();
  //清空队列
  void queue_delete();
  //判空
  bool queue_empty();
  //判满
  bool queue_full();
  //求队列的大小
  int queue_size();
  //遍历
  void queue_show();
};

#endif // QUEUE_H

源文件:

cpp 复制代码
#include"queue.h"

//构造函数
queue::queue()
{
    cout<<"构造函数"<<endl;
}
//析构函数
queue::~queue()
{
    delete[] data;
    cout<<"析构函数"<<endl;
}
//拷贝构造函数
queue::queue(const queue &other )
{
    int index=front;
    while(index!=tail)
    {
        data[index]=other.data[index];
        index=(index+1)%128;
    }
    cout<<"拷贝构造函数"<<endl;
}
//入队
int queue:: queue_push(int e)
{
    if(queue_full())
    {
        return -1;
    }
    data[tail]=e;
    tail=(tail+1)%128;
    return 0;

}
//出队
int queue::queue_pop()
{
    if(queue_empty())
    {
        return -1;
    }
    front=(front+1)%128;
    return 0;
}
//清空队列
void queue::queue_delete()
{
    tail=front=0;
}
//判空
bool queue::queue_empty()
{
    return front==tail;
}
//判满
bool queue::queue_full()
{
    return (tail+1)%128==front;
}
//求队列的大小
int queue::queue_size()
{
    return (tail-front+128)%128;
}

//遍历
void queue::queue_show()
{
    for(int i=front;i!=tail;i=(i+1)%128)
    {
        cout<<data[i]<<" ";
    }
}

测试文件:

cpp 复制代码
#include"queue.h"

int main()
{
    queue q1;
    q1.queue_push(520);
    q1.queue_push(1314);
    q1.queue_pop();
    cout<<"q1的大小为"<<q1.queue_size()<<endl;
    q1.queue_show();
    return 0;
}

运行结果:

思维导图

相关推荐
Once_day7 分钟前
CC++八股文之内存
c语言·c++
量子炒饭大师20 分钟前
【C++入门】Cyber骇客的同名异梦——【C++重载函数】(与C的函数差异)
c语言·开发语言·c++·函数重载
charlie11451419123 分钟前
现代嵌入式C++教程:if constexpr——把编译期分支写得像写注释 —— 工程味实战指南
开发语言·c++·笔记·学习·嵌入式·现代c++
LIZhang201625 分钟前
c++ 转化句柄,解决多线程安全释放问题
开发语言·c++
youqingyike36 分钟前
Qt 中 QWidget 调用setLayout 后不显示
开发语言·c++·qt
_OP_CHEN1 小时前
【从零开始的Qt开发指南】(二十二)Qt 音视频开发宝典:从音频播放到视频播放器的实战全攻略
开发语言·c++·qt·音视频·前端开发·客户端开发·gui开发
oioihoii1 小时前
从C++到C#的转型完全指南
开发语言·c++·c#
学嵌入式的小杨同学1 小时前
C 语言实战:动态规划求解最长公共子串(连续),附完整实现与优化
数据结构·c++·算法·unity·游戏引擎·代理模式
小欣加油1 小时前
leetcode 174 地下城游戏
c++·算法·leetcode·职场和发展·动态规划
Two_brushes.1 小时前
Cmake中寻库文件的路径
开发语言·c++·cmake