• 【C语言 - 数据结构】浅析栈和队列


    对于线性表,我们可能要执行下列操作:访问表的第k个结点、在k这个结点之前或者之后插入一个新结点,抑或是删除第k个结点等等操作,其中我们会遇到值对第一个或者最后一个结点插入、删除、和访问值的线性表,我们给它们以特殊的名称:栈、队列、双端队列。

    让我们一起用热爱奔赴山海!

     

    文章目录

    • 一、栈、队列以及双端队列的概念
    • 二、栈的实现(动态)和模拟静态栈
    • 三、队列的实现(动态)和模拟静态队列
    • 四、leetcode-栈实现队列和用队列实现栈
    • 总结


    提示:以下是本篇文章正文内容,下面案例可供参考

    一、栈、队列以及双端队列的概念

    1.1栈的概念及结构

    栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端 称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

    压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

    出栈:栈的删除操作叫做出栈。出数据也在栈顶

     1.2队列的概念及结构

    队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out)

    入队列:进行插入操作的一端称为队尾

    出队列:进行删除操作的一端称为队头

    1.3双端队列的概念及结构

     双端队列:是一种线性表,又称为双向队列,所有的插入和删除(通常还有所有的访问)都在表的两端进行。

     

    二、栈的实现和模拟栈

    栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的 代价比较小。、

     

    2.1 实现一个支持动态增长的栈

    头文件:

    1. #pragma once
    2. #include<stdio.h>
    3. #include<stdlib.h>
    4. #include<stdbool.h>
    5. #include<assert.h>
    6. typedef int STDataType;
    7. typedef struct Stack//动态栈
    8. {
    9. int *a;
    10. int top;//栈顶的位置
    11. int capacity;//容量
    12. }ST;
    13. STDataType StackTop(ST* ps);//返回栈顶的值
    14. void StackInit(ST* ps);//初始化栈
    15. void StackDestory(ST* ps);//销毁栈
    16. void StackPop(ST* ps);//弹出
    17. void StackPush(ST* ps, STDataType x);//插入
    18. bool StackEmpty(ST* ps);//判断栈是否为空。

    源文件:

     

    1. #include"Stack.h"
    2. void StackInit(ST* ps)//栈的初始化
    3. {
    4. assert(ps);
    5. ps->a = NULL;//a点的值指向空
    6. ps->top = 0;//栈底为0
    7. ps->capacity = 0;//空间为0
    8. }
    9. void StackDestory(ST* ps)
    10. {
    11. assert(ps);
    12. free(ps->a);//把a释放掉
    13. ps->a = NULL;
    14. ps->capacity = ps->top = 0;
    15. }
    16. void StackPush(ST* ps, STDataType x)//入数据
    17. {
    18. assert(ps);
    19. //满了就扩容
    20. if (ps->top == ps->capacity)//如果栈的栈顶恰好和容量相等就扩容
    21. {
    22. int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    23. ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
    24. if (ps->a == NULL)
    25. {
    26. printf("realloc fail\n");
    27. exit(-1);
    28. }
    29. ps->capacity = newCapacity;//新的空间赋给旧的
    30. }
    31. ps->a[ps->top] = x;//栈顶插入x;
    32. ps->top++;//top++
    33. }
    34. void StackPop(ST* ps)
    35. {
    36. assert(ps);
    37. assert(ps->top > 0);
    38. --ps->top;//top--就相当于删除操作
    39. }
    40. bool StackEmpty(ST* ps)
    41. {
    42. assert(ps);
    43. //两种写法
    44. //if (ps->top > 0)
    45. //{
    46. // return false;
    47. //}
    48. //else
    49. //{
    50. // return true;
    51. //}
    52. return ps->top == 0;
    53. }
    54. STDataType StackTop(ST* ps)
    55. {
    56. assert(ps);
    57. assert(ps->top > 0);
    58. return ps->a[ps->top - 1];//访问栈顶元素(这里因为top我们设为0,所以访问栈顶元素相当于top-1
    59. }
    60. int StackSize(ST* ps)
    61. {
    62. assert(ps);
    63. return ps->top;
    64. }

     2.2数组模拟静态栈

    1. #include<iostream>
    2. using namespace std;
    3. const int N = 1e6 + 10;
    4. int n;
    5. int stk[N];
    6. int top = - 1;
    7. int main ()
    8. {
    9. cin >> n;
    10. while(n --)
    11. {
    12. string s;
    13. cin >> s;
    14. if(s == "push")
    15. {
    16. int a;
    17. cin >> a;
    18. stk[++top] = a;
    19. }
    20. if(s == "pop")
    21. {
    22. top--;
    23. }
    24. if(s == "empty")
    25. {
    26. if(top >= 0) printf("NO\n");
    27. else printf("YES\n");
    28. }
    29. if(s == "query")
    30. {
    31. printf("%d\n", stk[top]);
    32. }
    33. }
    34. return 0;
    35. }

    三、 队列的实现(动态)和模拟静态队列

    队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数 组头上出数据,效率会比较低。

     2.1 实现一个支持动态增长的栈

    头文件:

    1. #pragma once
    2. #include<stdio.h>
    3. #include<stdlib.h>
    4. #include<stdbool.h>
    5. #include<assert.h>
    6. typedef int QDataType;//方便改类型
    7. typedef struct QueueNode//保存每个节点的数据
    8. {
    9. QDataType data;
    10. struct QueueNode* next;
    11. }QNode;
    12. typedef struct Queue
    13. {
    14. QNode* head;
    15. QNode* tail;
    16. }Queue;
    17. //上面的写法等价于:
    18. //typedef struct Queue
    19. //{
    20. // QNode* head;
    21. // QNode* tail;
    22. //};
    23. //
    24. //typedef struct Queue Queue;//
    25. //一般实际情况哨兵位的头节点不存储值,不放数据
    26. void QueueInit(Queue* pq);//队列初始化
    27. void QueueDestory(Queue* pq);//队列销毁
    28. void QueuePush(Queue* pq, QDataType x);//队尾插入
    29. void QueuePop(Queue* pq);//弹出队头
    30. bool QueueEmpty(Queue* pq);//判断是否为空
    31. size_t QueueSize(Queue* pq);//size_t相当于Unsinged int
    32. QDataType QueueFront(Queue* pq);
    33. QDataType QueueBack(Queue* pq);

    源文件:

    1. #include"Queue.h"
    2. void QueueInit(Queue* pq)
    3. {
    4. assert(pq);
    5. pq->head = pq->tail = NULL;
    6. }
    7. void QueueDestory(Queue* pq)
    8. {
    9. assert(pq);
    10. QNode* cur = pq->head;
    11. while (cur)
    12. {
    13. QNode* next = cur->next;//先记录下一个位置
    14. free(cur);//free掉cur指针
    15. cur = next;//cur赋值到下一个位置
    16. }
    17. pq->head = pq->tail = NULL;//置空
    18. }
    19. void QueuePush(Queue* pq, QDataType x)//队尾插入//插入int类型的参数
    20. {
    21. assert(pq);
    22. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    23. assert(newnode);
    24. newnode->data = x;//新的节点的值被赋与x
    25. newnode->next = NULL;//新的节点是在队尾,所以指向的下一个位置是空
    26. if (pq->tail == NULL)//如果链表的第一个值为空,则head = tail = NULL
    27. {
    28. assert(pq->head == NULL);
    29. pq->head = pq->tail = newnode;
    30. }
    31. else//尾插
    32. {
    33. pq->tail->next = newnode;//先改指向
    34. pq->tail = newnode;//再改地址
    35. }
    36. }
    37. void QueuePop(Queue* pq)//弹出队首
    38. {
    39. assert(pq);
    40. assert(pq->head && pq->tail);
    41. if (pq->head->next == NULL)//只有一个节点
    42. {
    43. free(pq->head);
    44. pq->head = pq->tail = NULL;
    45. }
    46. else
    47. {
    48. QNode* next = pq->head->next;//QNode* next相当于是QDataType的头指针的下一个位置
    49. free(pq->head);
    50. pq->head = next;//头往后走
    51. }
    52. }
    53. bool QueueEmpty(Queue* pq)
    54. {
    55. assert(pq);
    56. //return pq->head == NULL && pq->tail == NULL;
    57. return pq->head == NULL;//程序调试了快一个小时就是因为pq->head没加后面的== NULL
    58. }
    59. size_t QueueSize(Queue* pq)//size_t相当于Unsinged int
    60. {
    61. assert(pq);
    62. QNode* cur = pq->head;
    63. size_t size = 0;
    64. while (cur)
    65. {
    66. size++;
    67. cur = cur->next;
    68. }
    69. return size;
    70. }
    71. QDataType QueueFront(Queue* pq)//返回队头第一个位的值
    72. {
    73. assert(pq);
    74. assert(pq->head);
    75. return pq->head->data;
    76. }
    77. QDataType QueueBack(Queue* pq)//返回队尾的值
    78. {
    79. assert(pq);
    80. assert(pq->tail);
    81. return pq->tail->data;
    82. }

     3.2数组模拟静态队列

     

    1. #include<iostream>
    2. #include<cstring>
    3. #include<algorithm>
    4. using namespace std;
    5. const int N = 1e5 + 10;
    6. int q[N];
    7. int n;
    8. int hh ,tt = -1;//hh表示头,tt表示尾
    9. int main ()
    10. {
    11. cin >> n;
    12. while(n --)
    13. {
    14. string s;
    15. cin >> s;
    16. if(s == "push")
    17. {
    18. int x;
    19. cin >> x;
    20. q[++tt] = x;
    21. }
    22. else if(s == "pop")
    23. {
    24. hh ++;
    25. }
    26. else if(s == "empty")
    27. {
    28. if(hh <= tt) printf("NO\n");//尾在逻辑上要比头更前面
    29. else printf("YES\n");
    30. }
    31. else cout << q[hh] << endl;
    32. }
    33. return 0;
    34. }

     四、leetcode-栈实现队列和用队列实现栈

     225. 用队列实现栈 - 力扣(LeetCode)

    代码:
     

    1. typedef int QDataType;
    2. typedef struct QueueNode//保存每个节点的数据
    3. {
    4. QDataType data;
    5. struct QueueNode* next;
    6. }QNode;
    7. typedef struct Queue
    8. {
    9. QNode* head;
    10. QNode* tail;
    11. }Queue;
    12. void QueueInit(Queue* pq);
    13. void QueueDestory(Queue* pq);
    14. void QueuePush(Queue* pq, QDataType x);//队尾插入
    15. void QueuePop(Queue* pq);
    16. bool QueueEmpty(Queue* pq);
    17. size_t QueueSize(Queue* pq);//size_t相当于Unsinged int
    18. QDataType QueueFront(Queue* pq);
    19. QDataType QueueBack(Queue* pq);
    20. void QueueInit(Queue* pq)
    21. {
    22. assert(pq);
    23. pq->head = pq->tail = NULL;
    24. }
    25. void QueueDestory(Queue* pq)
    26. {
    27. assert(pq);
    28. QNode* cur = pq->head;
    29. while (cur)
    30. {
    31. QNode* next = cur->next;//先记录下一个位置
    32. free(cur);//free掉cur指针
    33. cur = next;//cur赋值到下一个位置
    34. }
    35. pq->head = pq->tail = NULL;//置空
    36. }
    37. void QueuePush(Queue* pq, QDataType x)//队尾插入
    38. {
    39. assert(pq);
    40. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    41. assert(newnode);
    42. newnode->data = x;
    43. newnode->next = NULL;
    44. if (pq->tail == NULL)//如果链表的第一个值为空,则head = tail = NULL
    45. {
    46. assert(pq->head == NULL);
    47. pq->head = pq->tail = newnode;
    48. }
    49. else//尾插
    50. {
    51. pq->tail->next = newnode;
    52. pq->tail = newnode;
    53. }
    54. }
    55. void QueuePop(Queue* pq)//弹出队首
    56. {
    57. assert(pq);
    58. assert(pq->head && pq->tail);
    59. if (pq->head->next == NULL)//只有一个节点
    60. {
    61. free(pq->head);
    62. pq->head = pq->tail = NULL;
    63. }
    64. else
    65. {
    66. QNode* next = pq->head->next;//QNode* next相当于是QDataType的头指针的下一个位置
    67. free(pq->head);
    68. pq->head = next;//头往后走
    69. }
    70. }
    71. bool QueueEmpty(Queue* pq)
    72. {
    73. assert(pq);
    74. //return pq->head == NULL && pq->tail == NULL;
    75. return pq->head == NULL;//程序调试了快一个小时就是因为pq->head没加后面的== NULL
    76. }
    77. size_t QueueSize(Queue* pq)//size_t相当于Unsinged int
    78. {
    79. assert(pq);
    80. QNode* cur = pq->head;
    81. size_t size = 0;
    82. while (cur)
    83. {
    84. size++;
    85. cur = cur->next;
    86. }
    87. return size;
    88. }
    89. QDataType QueueFront(Queue* pq)//返回队头第一个位的值
    90. {
    91. assert(pq);
    92. assert(pq->head);
    93. return pq->head->data;
    94. }
    95. QDataType QueueBack(Queue* pq)
    96. {
    97. assert(pq);
    98. assert(pq->tail);
    99. return pq->tail->data;
    100. }
    101. typedef struct {
    102. Queue q1;
    103. Queue q2;
    104. } MyStack;
    105. MyStack* myStackCreate() {
    106. MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
    107. assert(pst);
    108. QueueInit(&pst->q1);
    109. QueueInit(&pst->q2);
    110. return pst;
    111. }
    112. void myStackPush(MyStack* obj, int x) {
    113. assert(obj);
    114. if(!QueueEmpty(&obj->q1))
    115. {
    116. QueuePush(&obj->q1, x);
    117. }
    118. else
    119. {
    120. QueuePush(&obj->q2, x);
    121. }
    122. }
    123. int myStackPop(MyStack* obj) {
    124. Queue* emptyQ = &obj->q1;//假设q1为空,q2不为空
    125. Queue* nonEmptyQ = &obj->q2;
    126. if(!QueueEmpty(&obj->q1))
    127. {
    128. emptyQ = &obj->q2;
    129. nonEmptyQ = &obj->q1;
    130. }
    131. //把非空队列的前N个数据导入空队列,剩下一个删掉
    132. //就实现了后进先出
    133. while(QueueSize(nonEmptyQ) > 1)
    134. {
    135. QueuePush(emptyQ, QueueFront(nonEmptyQ));
    136. QueuePop(nonEmptyQ);
    137. }
    138. int top = QueueFront(nonEmptyQ);//此时那个非空的队列只剩下一个数据
    139. QueuePop(nonEmptyQ);
    140. return top;
    141. }
    142. int myStackTop(MyStack* obj) {
    143. assert(obj);
    144. if(!QueueEmpty(&obj->q1))//如果q1不为空
    145. {
    146. return QueueBack(&obj->q1);
    147. }
    148. else
    149. {
    150. return QueueBack(&obj->q2);
    151. }
    152. }
    153. bool myStackEmpty(MyStack* obj) {
    154. return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
    155. }
    156. void myStackFree(MyStack* obj) {
    157. assert(obj);
    158. QueueDestory(&obj->q1);
    159. QueueDestory(&obj->q2);
    160. free(obj);
    161. }

     

     232. 用栈实现队列 - 力扣(LeetCode)栈是后进先出

    思路:设计两个栈,一个栈专门用来入数据,一个栈专门用来出数据。

     

    1. typedef int STDataType;
    2. typedef struct Stack//动态链表
    3. {
    4. int *a;
    5. int top;//栈顶的位置
    6. int capacity;//容量
    7. }ST;
    8. STDataType StackTop(ST* ps);
    9. void StackInit(ST* ps);//初始化栈
    10. void StackDestory(ST* ps);
    11. void StackPop(ST* ps);
    12. void StackPush(ST* ps, STDataType x);
    13. bool StackEmpty(ST* ps);
    14. void StackInit(ST* ps)
    15. {
    16. assert(ps);
    17. ps->a = NULL;
    18. ps->top = 0;
    19. ps->capacity = 0;
    20. }
    21. void StackDestory(ST* ps)
    22. {
    23. assert(ps);
    24. free(ps->a);
    25. ps->a = NULL;
    26. ps->capacity = ps->top = 0;
    27. }
    28. void StackPush(ST* ps, STDataType x)//入数据
    29. {
    30. assert(ps);
    31. //满了就扩容
    32. if (ps->top == ps->capacity)
    33. {
    34. int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    35. ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
    36. if (ps->a == NULL)
    37. {
    38. printf("realloc fail\n");
    39. exit(-1);
    40. }
    41. ps->capacity = newCapacity;
    42. }
    43. ps->a[ps->top] = x;
    44. ps->top++;
    45. }
    46. void StackPop(ST* ps)
    47. {
    48. assert(ps);
    49. assert(ps->top > 0);
    50. --ps->top;
    51. }
    52. bool StackEmpty(ST* ps)
    53. {
    54. assert(ps);
    55. //两种写法
    56. //if (ps->top > 0)
    57. //{
    58. // return false;
    59. //}
    60. //else
    61. //{
    62. // return true;
    63. //}
    64. return ps->top == 0;
    65. }
    66. STDataType StackTop(ST* ps)
    67. {
    68. assert(ps);
    69. assert(ps->top > 0);
    70. return ps->a[ps->top - 1];//访问栈顶元素
    71. }
    72. int StackSize(ST* ps)
    73. {
    74. assert(ps);
    75. return ps->top;
    76. }
    77. typedef struct
    78. {
    79. ST pushST;
    80. ST popST;
    81. } MyQueue;
    82. MyQueue* myQueueCreate() {
    83. MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    84. assert(obj);
    85. StackInit(&obj->pushST);//&符要加,要改变结构体里面的内容
    86. StackInit(&obj->popST);
    87. return obj;
    88. }
    89. void myQueuePush(MyQueue* obj, int x) {
    90. assert(obj);
    91. StackPush(&obj->pushST, x);
    92. }
    93. int myQueuePop(MyQueue* obj) {
    94. assert(obj);
    95. //如果popST为空, 把pushST的数据拿过来,就符合先进先出的顺序了
    96. if(StackEmpty(&obj->popST))//如果ST Pop为空就执行
    97. {
    98. while(!StackEmpty(&obj->pushST))
    99. {
    100. StackPush(&obj->popST, StackTop(&obj->pushST));
    101. StackPop(&obj->pushST);//把pushST里的数据删掉
    102. }
    103. }
    104. int front = StackTop(&obj->popST);//记录栈顶的数据
    105. StackPop(&obj->popST);
    106. return front;
    107. }
    108. int myQueuePeek(MyQueue* obj) {
    109. assert(obj);
    110. //如果popST为空, 把pushST的数据拿过来,就符合先进先出的顺序了
    111. if(StackEmpty(&obj->popST))//如果ST Pop为空就执行
    112. {
    113. while(!StackEmpty(&obj->pushST))
    114. {
    115. StackPush(&obj->popST, StackTop(&obj->pushST));
    116. StackPop(&obj->pushST);//把pushST里的数据删掉
    117. }
    118. }
    119. return StackTop(&obj->popST);
    120. }
    121. bool myQueueEmpty(MyQueue* obj) {
    122. assert(obj);
    123. return StackEmpty(&obj->pushST)&&StackEmpty(&obj->popST);
    124. }
    125. void myQueueFree(MyQueue* obj) {
    126. assert(obj);
    127. StackDestory(&obj->pushST);
    128. StackDestory(&obj->popST);
    129. free(obj);
    130. }

     


    总结

     本文分别从(1)栈、队列以及双端队列的概念、(2)栈的实现(动态)和模拟静态栈、(3)队列的实现(动态)和模拟静态队列、(4)leetcode-栈实现队列和用队列实现栈四个方面对栈和队列这两个数据结构进行了分析,希望大家读后能够有所收获,也希望大家多多支持。

  • 相关阅读:
    Transformer,浅析归纳偏置对模型缩放的影响
    vite vue项目 运行时 \esbuild\esbuild.exe 缺失 错误码 errno: -4058, code: ‘ENOENT‘,
    【C++】模板-非类型模板参数
    阿里企业邮箱域名解析MX记录表
    智领未来,安全无忧:智能网联汽车监控大屏的守护之旅
    14.MongoDB导出备份
    如何在单链表中的任意一个位置插入一个结点
    【MySql】Mysql之备份与恢复
    静态路由——实现两个不相连的网段通信实验
    编译
  • 原文地址:https://blog.csdn.net/qq_62662919/article/details/124898215