包含私有成员属性:栈的数组、记录栈顶的变量
成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小
- #include
- #define MAX 128
-
- using namespace std;
-
-
- class Stack
- {
- private:
- int *p = new int[MAX];//栈的数组
- int top;//记录栈顶的下标
- public:
- //构造函数
- Stack()
- {
- top = -1;
- cout<<"无参构造函数"<
- }
- //析构函数
- ~Stack()
- {
- cout<<"Stack::析构函数"<
- }
- //拷贝构造函数
- Stack(const Stack &other):p(other.p),top(other.top)
- {
- cout<<"拷贝构造函数"<
- }
- //入栈
- bool stack_push(int e)
- {
- if(stack_full())
- {
- cout<<"入栈失败"<
- return false;
- }
- top++;
- p[top] = e;
- cout<
" 入栈成功"< -
- return true;
- }
- //出栈
- bool stack_pop()
- {
- if(stack_empty())
- {
- return false;
- }
- int e = p[top];
-
- top--;
- cout<
" 出栈成功"< -
- return true;
- }
- //清空栈
- void stack_free()
- {
- while(top != -1)
- {
- stack_pop();
- }
- delete [] p;
- p = nullptr;
- cout<<"清空成功"<
- }
- //判空
- int stack_empty()
- {
- return top == -1;
- }
- //判满
- int stack_full()
- {
- return top == MAX-1;
-
- }
- //获取栈顶元素
- int stack_top()
- {
- if(stack_empty())
- {
- cout<<"获取失败"<
- return -1;
- }
- cout<<"栈顶元素为 "<
- return p[top];
- }
-
- //栈的大小
- int stack_size()
- {
- cout<<"栈的大小为 "<
1< - return top+1;
- }
- };
-
-
- int main()
- {
- Stack m;
-
- m.stack_push(6);
- m.stack_push(7);
- m.stack_push(4);
- m.stack_push(8);
- m.stack_push(9);
-
- m.stack_pop();
- m.stack_top();
- m.stack_size();
- m.stack_free();
-
- return 0;
- }

2>自行封装一个循环顺序队列的类
包含私有成员属性:存放队列的数组、队头位置、队尾位置
成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小
- #include
- #define MAX 128
-
- using namespace std;
- class Queue
- {
- private:
- int *p=new int[MAX];//队列的数组
- int tail;//记录队尾元素
- int head;//记录对头元素
- public:
- //构造函数
- Queue(int t=0)
- {
- head=t;
- tail=t;
- cout<<"无参构造函数"<
- }
- //析构函数
- ~Queue()
- {
- cout<<"Stack::析构函数"<
- }
- //拷贝构造函数
- Queue(const Queue &other):p(other.p),tail(other.tail),head(other.head)
- {
- cout<<"拷贝构造函数"<
- }
- //入队
- int queue_push(int e)
- {
- if(queue_full())
- {
- cout<<"入队失败"<
- return -1;
- }
- p[tail]=e;
- tail++;
- cout<<"入队成功"<
- return 0;
- }
- //出队
- int queue_pop()
- {
- if(queue_empty())
- {
- cout<<"出队失败"<
- return -1;
- }
- int e = p[head];
- head = (head+1)%MAX;
- cout<
" 出队成功"< - return 0;
- }
- //清空队列
- bool queue_delete()
- {
- while(head!=tail)
- {
- queue_pop();
- }
- delete [] p;
- p=nullptr;
- cout<<"清空队列成功"<
- return true;
- }
- //判空
- bool queue_empty()
- {
- if(head==tail)
- {
- cout<<"队列空"<
- return true;
- }
- return false;
- }
- //判满
- bool queue_full()
- {
- if((tail+1)==0)
- {
- cout<<"队列满了"<
- return true;
- }
- return false;
- }
- //队列的大小
- void queue_getsize()
- {
- int size;
- size=(tail-head+MAX)%MAX;
- cout<<"队的大小为:"<
- }
- void show(int i)
- {
- cout<
" ";
- }
- };
- int main()
- {
- Queue q1;
- int e;
- int s;
- q1.queue_empty();
- cout<<"请输入要入队的个数:";
- cin>>s;
- for(int i=0;i
- {
- cout<<"请输入要入队的元素:";
- cin>>e;
- q1.queue_push(e);
- }
- q1.queue_getsize();
- for(int i=0;i
- {
- q1.show(i);
- }
- cout<
- q1.queue_delete();
- return 0;
- }


-
相关阅读:
使用 MyBatis 日志插件实现日志记录
聊聊芯片制造中的金属杂质
Keil 厂商DFP pack实现原理
LeetCode每日一题(2196. Create Binary Tree From Descriptions)
SprIngSecurity实战(二)各种架构中所应用的安全性
java 并发执行批量异步任务(Future、 CompletableFuture 实现)
Airpods的电池健不健康不直观,但例行检查很重要!如何检查Airpods的电池健康状况
【详细学习SpringBoot核心源码之SpringApplication构造器&Run方法源码详细流程-4】
es6运算符扩展
牛血清白蛋白包裹金纳米簇
-
原文地址:https://blog.csdn.net/qq_53478460/article/details/132779228