• day43:C++ day3 类、结构体与类的区别、this指针、类中特殊成员函数


    1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量

    成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

    stack.h:

    1. #ifndef STACK_H
    2. #define STACK_H
    3. #include
    4. using namespace std;
    5. #define MAX 50
    6. class Stack
    7. {
    8. private:
    9. int *data;
    10. int top;
    11. public:
    12. Stack():data(new int[MAX]),top(-1)
    13. {
    14. cout<<"Stack::无参构造函数"<
    15. }
    16. ~Stack()
    17. {
    18. cout<<"Stack::构析函数"<
    19. }
    20. Stack(const Stack &other)
    21. {
    22. this->data=new int[MAX];
    23. memcpy(this->data,other.data,sizeof(int)*MAX);
    24. this->top=other.top;
    25. cout<<"Stack::拷贝构造函数"<
    26. }
    27. void stack_push(Stack &S,int &&val);
    28. void stack_pop(Stack &S);
    29. void stack_clear(Stack &S);
    30. bool stack_empty(Stack &S);
    31. bool stack_full(Stack &S);
    32. int stack_gettop(Stack &S);
    33. int stack_len(Stack &S);
    34. void stack_show(Stack &S);
    35. };
    36. #endif // STACK_H

    stack.cpp:

    1. #include "stack.h"
    2. void Stack::stack_push(Stack &S,int &&val)
    3. {
    4. S.top++;
    5. S.data[top]=val;
    6. }
    7. void Stack::stack_pop(Stack &S)
    8. {
    9. if(NULL==S.data || stack_empty(S))
    10. {
    11. cout<<"所给顺序栈不合法"<
    12. }
    13. cout<"出栈成功"<
    14. S.top--;
    15. }
    16. void Stack::stack_clear(Stack &S)
    17. {
    18. while(!stack_empty(S))
    19. {
    20. stack_pop(S);
    21. }
    22. }
    23. bool Stack::stack_empty(Stack &S)
    24. {
    25. return -1==top;
    26. }
    27. bool Stack::stack_full(Stack &S)
    28. {
    29. return MAX-1==S.top;
    30. }
    31. int Stack::stack_gettop(Stack &S)
    32. {
    33. return S.data[top];
    34. }
    35. int Stack::stack_len(Stack &S)
    36. {
    37. return top+1;
    38. }
    39. void Stack::stack_show(Stack &S)
    40. {
    41. for(int i=top;i>=0;i--)
    42. {
    43. cout<" ";
    44. }
    45. cout<
    46. }

    main.cpp:

    1. #include"stack.h"
    2. int main()
    3. {
    4. Stack S;
    5. cout<stack_empty(S)<
    6. S.stack_push(S,5);
    7. S.stack_push(S,8);
    8. S.stack_push(S,6);
    9. S.stack_push(S,7);
    10. S.stack_push(S,3);
    11. S.stack_push(S,9);
    12. cout<stack_full(S)<
    13. cout<<"栈顶元素为"<stack_gettop(S)<
    14. cout<<"栈的长度为"<stack_len(S)<
    15. S.stack_show(S);
    16. S.stack_clear(S);
    17. S.stack_push(S,520);
    18. S.stack_show(S);
    19. return 0;
    20. }

    2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置

    成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

    queue.h:

    1. #ifndef QUEUE_H
    2. #define QUEUE_H
    3. #include
    4. #define MAX 50
    5. using namespace std;
    6. class Queue
    7. {
    8. private:
    9. int *data;
    10. int head;
    11. int tail;
    12. public:
    13. Queue():data(new int[MAX]),head(0),tail(0)
    14. {
    15. cout<<"queue::无参构造函数"<
    16. }
    17. ~Queue()
    18. {
    19. delete []data;
    20. cout<<"queue::析构函数"<
    21. }
    22. Queue(const Queue &other):
    23. data(new int(*other.data)),
    24. head(other.head),
    25. tail(other.tail)
    26. {
    27. this->data=new int[MAX];
    28. memcpy(this->data,other.data,sizeof(int)*MAX);
    29. /*
    30. for(int i=other.head;i!=other.tail;i=(i+1)%MAX)
    31. {
    32. data[i]=other.data[i];
    33. }
    34. */
    35. this->head=other.head;
    36. this->tail=other.tail;
    37. cout<<"拷贝构造函数"<
    38. }
    39. void queue_push(Queue &Q,int &&val);
    40. void queue_pop(Queue &Q);
    41. void queue_clear(Queue &Q);
    42. bool queue_empty(Queue &Q);
    43. bool queue_full(Queue &Q);
    44. int queue_len(Queue &Q);
    45. void queue_show(Queue &Q);
    46. };
    47. #endif // QUEUE_H

    queue.cpp:

    1. #include "queue.h"
    2. void Queue::queue_push(Queue &Q,int &&val)
    3. {
    4. Q.data[tail]=val;
    5. tail=(tail+1)%MAX;
    6. return;
    7. }
    8. void Queue::queue_pop(Queue &Q)
    9. {
    10. cout<"出队成功"<
    11. head=(head+1)%MAX;
    12. return;
    13. }
    14. void Queue::queue_clear(Queue &Q)
    15. {
    16. while(!queue_empty(Q))
    17. {
    18. queue_pop(Q);
    19. }
    20. }
    21. bool Queue::queue_empty(Queue &Q)
    22. {
    23. return Q.head==Q.tail;
    24. }
    25. bool Queue::queue_full(Queue &Q)
    26. {
    27. return Q.head==(Q.tail+MAX)%MAX;
    28. }
    29. int Queue::queue_len(Queue &Q)
    30. {
    31. return (Q.tail+MAX-Q.head)%MAX;
    32. }
    33. void Queue::queue_show(Queue &Q)
    34. {
    35. for(int i=Q.head;i!=Q.tail;i=(i+1)%MAX)
    36. {
    37. cout<" ";
    38. }
    39. cout<
    40. }

    main.c

    1. #include "queue.h"
    2. int main()
    3. {
    4. Queue Q;
    5. Q.queue_empty(Q);
    6. Q.queue_push(Q,5);
    7. Q.queue_push(Q,8);
    8. Q.queue_push(Q,5);
    9. Q.queue_push(Q,8);
    10. Q.queue_full(Q);
    11. Q.queue_pop(Q);
    12. Q.queue_show(Q);
    13. Q.queue_clear(Q);
    14. Queue L;
    15. L=Q;
    16. L.queue_show(L);
    17. return 0;
    18. }

    思维导图:有道笔记

  • 相关阅读:
    起重机笔记
    《MySQL高级篇》六、索引的创建与设计原则
    代码优化个人经验总结(以代码解耦模块化 减少代码量为目标 提高可维护性降低bug率)
    jupyter notebook anaconda环境下查看|加载|更换内核
    国家网络安全周2023时间是什么时候?有什么特点?谁举办的?
    Java 字节输出流FileOutputStream的用法和概述
    非互联网客户收入近6成,阿里云进入新周期
    uniapp列表进入动画
    如何实现实时音视频聊天功能
    nginx实现负载均衡load balance
  • 原文地址:https://blog.csdn.net/wxmchong/article/details/132797492