1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量
成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小
stack.h:
- #ifndef STACK_H
- #define STACK_H
-
- #include
- using namespace std;
- #define MAX 50
- class Stack
- {
- private:
- int *data;
- int top;
- public:
- Stack():data(new int[MAX]),top(-1)
- {
- cout<<"Stack::无参构造函数"<
- }
- ~Stack()
- {
- cout<<"Stack::构析函数"<
- }
- Stack(const Stack &other)
- {
- this->data=new int[MAX];
- memcpy(this->data,other.data,sizeof(int)*MAX);
- this->top=other.top;
- cout<<"Stack::拷贝构造函数"<
- }
- void stack_push(Stack &S,int &&val);
- void stack_pop(Stack &S);
- void stack_clear(Stack &S);
- bool stack_empty(Stack &S);
- bool stack_full(Stack &S);
- int stack_gettop(Stack &S);
- int stack_len(Stack &S);
- void stack_show(Stack &S);
- };
- #endif // STACK_H
stack.cpp:
- #include "stack.h"
- void Stack::stack_push(Stack &S,int &&val)
- {
- S.top++;
- S.data[top]=val;
- }
- void Stack::stack_pop(Stack &S)
- {
- if(NULL==S.data || stack_empty(S))
- {
- cout<<"所给顺序栈不合法"<
- }
- cout<
"出栈成功"< - S.top--;
- }
- void Stack::stack_clear(Stack &S)
- {
- while(!stack_empty(S))
- {
- stack_pop(S);
- }
- }
- bool Stack::stack_empty(Stack &S)
- {
- return -1==top;
- }
- bool Stack::stack_full(Stack &S)
- {
- return MAX-1==S.top;
- }
- int Stack::stack_gettop(Stack &S)
- {
- return S.data[top];
- }
- int Stack::stack_len(Stack &S)
- {
- return top+1;
- }
- void Stack::stack_show(Stack &S)
- {
- for(int i=top;i>=0;i--)
- {
- cout<
" "; - }
- cout<
- }
main.cpp:
- #include"stack.h"
-
- int main()
- {
- Stack S;
- cout<
stack_empty(S)< - S.stack_push(S,5);
- S.stack_push(S,8);
- S.stack_push(S,6);
- S.stack_push(S,7);
- S.stack_push(S,3);
- S.stack_push(S,9);
- cout<
stack_full(S)< - cout<<"栈顶元素为"<
stack_gettop(S)< - cout<<"栈的长度为"<
stack_len(S)< - S.stack_show(S);
- S.stack_clear(S);
- S.stack_push(S,520);
- S.stack_show(S);
- return 0;
- }
2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置
成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小
queue.h:
- #ifndef QUEUE_H
- #define QUEUE_H
- #include
-
- #define MAX 50
- using namespace std;
- class Queue
- {
- private:
- int *data;
- int head;
- int tail;
-
- public:
- Queue():data(new int[MAX]),head(0),tail(0)
- {
- cout<<"queue::无参构造函数"<
- }
- ~Queue()
- {
- delete []data;
- cout<<"queue::析构函数"<
- }
- Queue(const Queue &other):
- data(new int(*other.data)),
- head(other.head),
- tail(other.tail)
- {
- this->data=new int[MAX];
- memcpy(this->data,other.data,sizeof(int)*MAX);
- /*
- for(int i=other.head;i!=other.tail;i=(i+1)%MAX)
- {
- data[i]=other.data[i];
- }
- */
- this->head=other.head;
- this->tail=other.tail;
- cout<<"拷贝构造函数"<
- }
- void queue_push(Queue &Q,int &&val);
- void queue_pop(Queue &Q);
- void queue_clear(Queue &Q);
- bool queue_empty(Queue &Q);
- bool queue_full(Queue &Q);
- int queue_len(Queue &Q);
- void queue_show(Queue &Q);
- };
- #endif // QUEUE_H
queue.cpp:
- #include "queue.h"
- void Queue::queue_push(Queue &Q,int &&val)
- {
- Q.data[tail]=val;
- tail=(tail+1)%MAX;
- return;
- }
- void Queue::queue_pop(Queue &Q)
- {
- cout<
"出队成功"< - head=(head+1)%MAX;
- return;
- }
- void Queue::queue_clear(Queue &Q)
- {
- while(!queue_empty(Q))
- {
- queue_pop(Q);
- }
- }
- bool Queue::queue_empty(Queue &Q)
- {
- return Q.head==Q.tail;
- }
- bool Queue::queue_full(Queue &Q)
- {
- return Q.head==(Q.tail+MAX)%MAX;
- }
- int Queue::queue_len(Queue &Q)
- {
- return (Q.tail+MAX-Q.head)%MAX;
- }
- void Queue::queue_show(Queue &Q)
- {
- for(int i=Q.head;i!=Q.tail;i=(i+1)%MAX)
- {
- cout<
" "; - }
- cout<
- }
main.c
- #include "queue.h"
-
- int main()
- {
- Queue Q;
- Q.queue_empty(Q);
- Q.queue_push(Q,5);
- Q.queue_push(Q,8);
- Q.queue_push(Q,5);
- Q.queue_push(Q,8);
- Q.queue_full(Q);
- Q.queue_pop(Q);
- Q.queue_show(Q);
- Q.queue_clear(Q);
- Queue L;
- L=Q;
- L.queue_show(L);
- return 0;
- }
思维导图:有道笔记
-
相关阅读:
起重机笔记
《MySQL高级篇》六、索引的创建与设计原则
代码优化个人经验总结(以代码解耦模块化 减少代码量为目标 提高可维护性降低bug率)
jupyter notebook anaconda环境下查看|加载|更换内核
国家网络安全周2023时间是什么时候?有什么特点?谁举办的?
Java 字节输出流FileOutputStream的用法和概述
非互联网客户收入近6成,阿里云进入新周期
uniapp列表进入动画
如何实现实时音视频聊天功能
nginx实现负载均衡load balance
-
原文地址:https://blog.csdn.net/wxmchong/article/details/132797492