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;
- }
运行结果:

思维导图

-
相关阅读:
防火墙实验2
java-php-python-springboot校园博客系统计算机毕业设计
使用Zadig从0到1搭建持续交付平台
微服务基础,分布式核心,常见微服务框架,SpringCloud概述,搭建SpringCloud微服务项目详细步骤,含源代码
c++_learning-对象模型探索
TPM分析笔记(七)Start-up相关命令字
21款奔驰GLS400升级电吸门 告别异响
Hadoop发展史
6.MySql连接SqlYog
下游批量推送的mysql库锁表
-
原文地址:https://blog.csdn.net/cwj442257772/article/details/132776474