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;
}

运行结果:

思维导图

相关推荐
AA陈超1 小时前
ASC学习笔记0014:手动添加一个新的属性集
c++·笔记·学习·ue5
Run_Teenage2 小时前
C++:智能指针的使用及其原理
开发语言·c++·算法
Mr_WangAndy4 小时前
C++_chapter15_C++重要知识点_auto,function,bind,decltype
c++·decltype·bind·function·可调用对象
万象.4 小时前
QT基础及对象树的认识
c++·qt
小张成长计划..6 小时前
【C++】2:cin和cout的介绍和使用,函数的缺省参数
c++
再卷也是菜6 小时前
C++篇(17)哈希拓展学习
c++·哈希
“愿你如星辰如月”7 小时前
Linux:进程间通信
linux·运维·服务器·c++·操作系统
灵晔君7 小时前
C++标准模板库(STL)——list的模拟实现
c++·list
Justinyh8 小时前
1、CUDA 编程基础
c++·人工智能
white-persist9 小时前
差异功能定位解析:C语言与C++(区别在哪里?)
java·c语言·开发语言·网络·c++·安全·信息可视化