• 栈和队列的基本操作


    (一)实验类型:设计性

    (二)实验目的:

          1.掌握栈和队列的抽象数据类型。

    2.掌握实现栈和队列的各种操作的算法。

    3.理解栈与递归的关系。

    4. 掌握队列的链式存贮结构及基本操作,深入了解链队列的基本特性,以便在实际问题背景下灵活运用它们。

    (三)实验内容:

    1. 栈和队列的数据结构定义;

    2. 栈的建立、初始化、判空、入栈、出栈等操作。

      3. 队列的建立、初始化、判空、入队、出队等操作。

    1. //1. 栈和队列的数据结构定义:
    2. // 栈的数据结构定义
    3. typedef struct {
    4. int top; // 栈顶指针
    5. int capacity; // 栈容量
    6. int* array; // 栈数据
    7. } Stack;
    8. // 队列的数据结构定义
    9. typedef struct {
    10. int front; // 队首指针
    11. int rear; // 队尾指针
    12. int capacity; // 队列容量
    13. int* array; // 队列数据
    14. } Queue;
    15. //在C / C++中,数组名本质上是一个指向数组首元素的指针。
    16. //因此,当使用 int* array; 声明一个指针时,array指向的是一个int类型的内存空间,
    17. //它可以被用来存储整数数据,也可以被当做数组使用。
    18. //2. 栈的建立、初始化、判空、入栈、出栈等操作:
    19. // 创建一个新的栈
    20. Stack * createStack(int capacity) {
    21. Stack* stack = (Stack*)malloc(sizeof(Stack));
    22. stack->capacity = capacity;
    23. stack->top = -1;
    24. stack->array = (int*)malloc(capacity * sizeof(int));
    25. //需要内存的访问,要访问容量才能设置合理的内存空间
    26. return stack;
    27. }
    28. // 判断栈是否为空
    29. int isStackEmpty(Stack* stack) {
    30. return stack->top == -1;
    31. }
    32. // 判断栈是否已满
    33. int isStackFull(Stack* stack) {
    34. return stack->top == stack->capacity - 1;
    35. }
    36. //从-1开始,自然要-1
    37. // 入栈
    38. void push(Stack* stack, int item) {
    39. if (isStackFull(stack)) {
    40. printf("栈已满,无法入栈。\n");
    41. return;
    42. }
    43. stack->array[++stack->top] = item;
    44. // stack->top是一个整体,表示栈顶
    45. //然后才是++,表示插入下一个位置
    46. printf("%d 入栈成功。\n", item);
    47. }
    48. // 出栈
    49. int pop(Stack* stack) {
    50. if (isStackEmpty(stack)) {
    51. printf("栈为空,无法出栈。\n");
    52. return -1;
    53. }
    54. int item = stack->array[stack->top--];
    55. //只需要把Top数减一即可
    56. printf("%d 出栈成功。\n", item);
    57. return item;
    58. }
    59. // 清空栈
    60. void clearStack(Stack* stack) {
    61. stack->top = -1;
    62. }
    63. // 销毁栈
    64. void destroyStack(Stack* stack) {
    65. free(stack->array);
    66. free(stack);
    67. }
    68. ```
    69. //3. 队列的建立、初始化、判空、入队、出队等操作:
    70. // 创建一个新的队列
    71. Queue * createQueue(int capacity) {
    72. Queue* queue = (Queue*)malloc(sizeof(Queue));
    73. queue->capacity = capacity;
    74. queue->front = queue->rear = -1;
    75. queue->array = (int*)malloc(capacity * sizeof(int));
    76. return queue;
    77. }
    78. // 判断队列是否为空
    79. int isQueueEmpty(Queue* queue) {
    80. return queue->front == -1;
    81. }
    82. // 判断队列是否已满
    83. int isQueueFull(Queue* queue) {
    84. return (queue->rear + 1) % queue->capacity == queue->front;
    85. }
    86. // 入队
    87. void enqueue(Queue* queue, int item) {
    88. if (isQueueFull(queue)) {
    89. printf("队列已满,无法入队。\n");
    90. return;
    91. }
    92. if (isQueueEmpty(queue)) {
    93. queue->front = queue->rear = 0;
    94. }
    95. else {
    96. queue->rear = (queue->rear + 1) % queue->capacity;
    97. //分配一个位置给新的数据
    98. }
    99. queue->array[queue->rear] = item;
    100. printf("%d 入队成功。\n", item);
    101. }
    102. // 出队
    103. int dequeue(Queue* queue) {
    104. if (isQueueEmpty(queue)) {
    105. printf("队列为空,无法出队。\n");
    106. return -1;
    107. }
    108. int item = queue->array[queue->front];//队头出队
    109. if (queue->front == queue->rear) //相等时,说明队列中仅有一个元素,所以出队完该元素后就直接设置为队空即可
    110. {
    111. queue->front = queue->rear = -1;
    112. }
    113. else {
    114. queue->front = (queue->front + 1) % queue->capacity;//直接替换成下一个front
    115. }
    116. printf("%d 出队成功。\n", item);
    117. return item;
    118. }
    119. // 清空队列
    120. void clearQueue(Queue* queue) {
    121. queue->front = queue->rear = -1;
    122. }
    123. // 销毁队列
    124. void destroyQueue(Queue* queue) {
    125. free(queue->array);
    126. free(queue);
    127. }

  • 相关阅读:
    计算机操作系统学习(六)设备管理
    初学python记录:力扣216. 组合总和 III
    C++学习第六课--string类型操作笔记
    HIve数仓新零售项目ODS层的构建
    使用Kubebuilder编写operator
    我国融资租赁行业有望达到13万亿元 互融云融资租赁系统助力行业稳健发展
    Java EnumSet clone()方法具有什么功能
    第二证券:特斯拉将推出低价电动汽车?最新消息
    模拟一个js底层数据类型隐式转换
    不使用实体类的情况下接收SQL查询结果、@Autowired注入为null解决
  • 原文地址:https://blog.csdn.net/ASBSIHD/article/details/133658380