1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量
成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小
头文件stack.c
- #ifndef STACK_H
- #define STACK_H
- #include
- #include
-
- 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
源文件:
- #include"stack.h"
- //构造函数
- stack::stack()
- {
- cout<<"构造函数"<
- }
- //析构函数
- stack::~stack()
- {
- delete[] data;
- cout<<"析构函数"<
- }
- //拷贝构造函数
- stack::stack(const stack &other)
- {
-
- cout<<"拷贝构造函数"<
- }
- //入栈
- int stack::stack_push(int e)
- {
- top++;
- data[top]=e;
- cout<<"入栈成功"<
- return 0;
- }
- //出栈
- int stack::stack_pop()
- {
- top--;
- cout<<"出栈成功"<
- 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;
- }
测试文件:
- #include"stack.h"
-
- int main()
- {
- stack s1;
- s1.stack_push(1);
- s1.stack_push(2);
- s1.stack_pop();
- cout << "s1.data[top]="<
stack_top()<< endl; - return 0;
- }
运行结果:

2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置
成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小
头文件:
- #ifndef QUEUE_H
- #define QUEUE_H
- #include
- 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
源文件:
- #include"queue.h"
-
- //构造函数
- queue::queue()
- {
- cout<<"构造函数"<
- }
- //析构函数
- queue::~queue()
- {
- delete[] data;
- cout<<"析构函数"<
- }
- //拷贝构造函数
- queue::queue(const queue &other )
- {
- int index=front;
- while(index!=tail)
- {
- data[index]=other.data[index];
- index=(index+1)%128;
- }
- cout<<"拷贝构造函数"<
- }
- //入队
- 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<" ";
- }
- }
测试文件:
- #include"queue.h"
-
- int main()
- {
- queue q1;
- q1.queue_push(520);
- q1.queue_push(1314);
- q1.queue_pop();
- cout<<"q1的大小为"<
queue_size()< - q1.queue_show();
- return 0;
- }
运行结果:

思维导图

-
相关阅读:
链表OJ题(1)
vue 内置指令-v-pre/v-memo
容器化 | 使用 Alpine 构建 Redis 镜像
Deepsort工作原理分析
在线工具推荐!能在线解决的问题,何必下载软件呢?
Spring Boot 目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&(CVE-2022-22947)&&(CVE-2022-2296)
这款信创FTP软件,可实现安全稳定的文件传输
Eureka 相关配置及特性
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
Spring boot 实战指南(一):入门、配置、Web、文件上传、异常页面
-
原文地址:https://blog.csdn.net/cwj442257772/article/details/132776474
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU