• c++day3


    stack.h

    1. #ifndef STACK_H
    2. #define STACK_H
    3. #include
    4. //#define max 128
    5. using namespace std;
    6. class Stack
    7. {
    8. private:
    9. int* stack;//数组指针
    10. int top;//栈顶元素
    11. int max;//栈容量
    12. public:
    13. //构造函数
    14. Stack();
    15. //析构函数
    16. ~Stack();
    17. //定义拷贝构造函数
    18. Stack(const Stack &other);
    19. //入栈
    20. void push(int item);
    21. //出栈
    22. int pop();
    23. //清空栈
    24. void clear();
    25. //遍历栈
    26. void stack_show();
    27. //判空
    28. bool empty();
    29. //判满
    30. bool full();
    31. //获取栈顶元素
    32. int stack_top();
    33. //求栈的大小
    34. int stack_size();
    35. };
    36. #endif // STACK_H

    stack.c

    1. #include"stack.h"
    2. //构造函数
    3. Stack::Stack():stack(nullptr),top(-1),max(128)
    4. {
    5. stack = new int[max];
    6. for(int i=0;i
    7. {
    8. stack[i]=0;
    9. }
    10. }
    11. //拷贝函数
    12. Stack::Stack(const Stack& other)
    13. {
    14. max = other.max;
    15. stack = new int[other.max];
    16. for(int i = 0; i < other.max; i++)
    17. {
    18. stack[i] = other.stack[i];
    19. }
    20. }
    21. //入栈
    22. void Stack::push(int item)
    23. {
    24. if (full())
    25. {
    26. cout << "Stack is full." << endl;
    27. return;
    28. }
    29. stack[++top] = item;
    30. cout << "push success" << endl;
    31. }
    32. //出栈
    33. int Stack::pop()
    34. {
    35. if (empty())
    36. {
    37. cout << "Stack is empty." << endl;
    38. return -1;
    39. }
    40. int item = stack[top--];
    41. return item;
    42. }
    43. //清空栈
    44. void Stack::clear()
    45. {
    46. top = -1;
    47. }
    48. //遍历栈
    49. void Stack::stack_show()
    50. {
    51. for(int i=0;i1;i++)
    52. {
    53. cout<
    54. }
    55. }
    56. //判空
    57. bool Stack::empty()
    58. {
    59. return top == -1;
    60. }
    61. //判满
    62. bool Stack::full()
    63. {
    64. return top == max-1;
    65. }
    66. //获取栈顶元素
    67. int Stack::stack_top()
    68. {
    69. if (empty())
    70. {
    71. cout << "Stack is empty." << endl;
    72. return -1;
    73. }
    74. return stack[top];
    75. }
    76. //求栈的大小
    77. int Stack::stack_size()
    78. {
    79. return top + 1;
    80. }

     queue.h

    1. #ifndef QUEUE_H
    2. #define QUEUE_H
    3. #include
    4. using namespace std;
    5. class Queue
    6. {
    7. public:
    8. //构造函数
    9. Queue(int size);
    10. //析构函数
    11. ~Queue();
    12. //拷贝函数
    13. Queue(const Queue& other);
    14. //入队
    15. bool enqueue(int a);
    16. //出队
    17. bool dequeue();
    18. //清空
    19. void clear();
    20. //判空
    21. bool isEmpty();
    22. //判满
    23. bool isFull();
    24. //获取队列大小
    25. int getSize();
    26. private:
    27. int* queue;//队列数组
    28. int size;//队列大小
    29. int front;//队头
    30. int rear;//队尾
    31. };
    32. #endif // QUEUE_H

     queue.c

    1. #include"queue.h"
    2. Queue::Queue(int size)
    3. {
    4. this->size = size;
    5. queue = new int[size]; // 根据队列容量动态分配数组内存
    6. front = -1; // 初始化队头位置为-1
    7. rear = -1; // 初始化队尾位置为-1
    8. }
    9. Queue::~Queue()
    10. {
    11. delete queue; // 释放数组内存空间
    12. }
    13. Queue::Queue(const Queue& other)
    14. {
    15. size = other.size; // 复制队列容量
    16. queue = new int[size]; // 根据队列容量动态分配数组内存
    17. front = other.front; // 复制队头位置
    18. rear = other.rear; // 复制队尾位置
    19. for (int i = front; i != rear; i = (i + 1) % size)
    20. {
    21. queue[i] = other.queue[i]; // 复制数组元素
    22. }
    23. queue[rear] = other.queue[rear]; // 复制数组元素
    24. }
    25. bool Queue::enqueue(int a)
    26. {
    27. if (isFull()) // 判断队列是否已满
    28. {
    29. cout << "Queue is full." << endl; // 输出错误信息
    30. return false; // 入队失败,返回false
    31. }
    32. if (isEmpty()) // 如果队列为空
    33. {
    34. front = 0; // 更新队头位置为0
    35. }
    36. rear = (rear + 1) % size; // 更新队尾位置,考虑循环
    37. queue[rear] = a; // 将元素a入队
    38. return true; // 入队成功,返回true
    39. }
    40. bool Queue::dequeue()
    41. {
    42. if (isEmpty()) // 判断队列是否为空
    43. {
    44. cout << "Queue is empty." << endl; // 输出错误信息
    45. return false; // 出队失败,返回false
    46. }
    47. if (front == rear) // 如果队列中只有一个元素
    48. {
    49. front = -1; // 更新队头位置为-1
    50. rear = -1; // 更新队尾位置为-1
    51. }
    52. else
    53. {
    54. front = (front + 1) % size; // 更新队头位置,考虑循环
    55. }
    56. return true; // 出队成功,返回true
    57. }
    58. void Queue::clear()
    59. {
    60. front = -1; // 清空队列,更新队头位置为-1
    61. rear = -1; // 清空队列,更新队尾位置为-1
    62. }
    63. bool Queue::isEmpty()
    64. {
    65. return front == -1 && rear == -1; // 判断队列是否为空,根据队头位置和队尾位置是否都为-1
    66. }
    67. bool Queue::isFull()
    68. {
    69. return (rear + 1) % size == front; // 判断队列是否已满,根据队尾位置加1取模后是否等于队头位置
    70. }
    71. int Queue::getSize()
    72. {
    73. if (isEmpty()) // 如果队列为空
    74. {
    75. return 0; // 返回队列大小为0
    76. }
    77. if (front <= rear) // 如果队头位置小于等于队尾位置
    78. {
    79. return rear - front + 1; // 返回队列大小为队尾位置减去队头位置再加1
    80. }
    81. else
    82. {
    83. return size - front + rear + 1; // 返回队列大小为队列容量减去队头位置再加上队尾位置再加1
    84. }
    85. }

  • 相关阅读:
    计算机毕业设计springboot+vue基本安卓/微信小程序的健康管理系统 uniapp
    MS35657步进电机驱动器可兼容DRV8824
    Mali GPU“补丁缺口”让 Android 用户容易受到攻击
    【微机原理与汇编语言】循环程序设计
    ES聚合与分组查询取值参数含义(Java api版本)
    redis学习-发布订阅
    Maven项目,进行编译,使用idea的 编译功能,就是正常的,但是在终端中执行 mvn clean compile 报错
    Python爬虫新手指南及简单实战
    UNIAPP day_01(8.30) uin-app概述
    外包干了3天,技术退步明显.......
  • 原文地址:https://blog.csdn.net/wdc857/article/details/132795452