• 数据结构之栈和队列


    要努力,但不要急。繁花锦簇,硕果累累都需要过程!

     1.栈

    1.栈的概念:

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

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

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

    2.栈的实现:

    栈的实现一般使用数据或者是链表实现,但一般情况下使用顺序表,因为数据在尾插的时候代价比较小。

    1.创建结构体:

    缺点:指定内存大小,无法改变,因此一般使用动态增长的栈:

     

     2.初始化栈:

    void StackInit(ST* ps);

     3.入栈:

    void StackPush(ST* ps, STDataType x);

    4.出栈:

     void StackPop(ST* ps);

     5.检查栈是否为空:

    bool StackEmpty(ST* ps);

     6.获取栈中有效元素的个数:

    int StackSize(ST* ps);

    7.获取栈顶的元素:

    STDataType StackTop(ST* ps);

     8.销毁栈:

     void StackDestroy(ST* ps);

     

    2.队列

    1.概念:

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

     

    2.队列的实现:

    队列的实现可以使用数组和链表,但是队列从头出数据,数组需要每次往前移动,时间复杂度为O(N),所以一般采用链表来实现队列

    1.创建链式结构:表示队列

    2.队列的结构:

     3.初始化队列:

    void QueueInit(Queue* pq);

    4.队列插入数据: 

    void QueuePush(Queue* pq, QDataType x);

    5.队列删除数据:

     void QueuePop(Queue* pq);

    6.判断队列是否为空:

     bool QueueEmpty(Queue* pq);

    7.获取队列头部的元素:

     QDataType QueueFront(Queue* pq);

    8.获取队列尾部的元素:

     QDataType QueueBack(Queue* pq);

    9.获取队列有效元素个数:

     int QueueSize(Queue* pq);

    10.销毁队列:

     void QueueDestroy(Queue* pq);

     

    3.栈和队列的面试题:

    题目1:

    括号匹配问题:

    oj链接

    解法思路:将左括号放入栈中,然后取出来一一和外边的比较

    不匹配的三种情况:

    1.栈中的元素为空

    2.栈中的元素不为空

    3.类型不匹配

    1. typedef char STDataType;
    2. typedef struct Stack
    3. {
    4. STDataType* arr;
    5. int top;
    6. int capacity;//记录数组的容量
    7. }ST;
    8. //初始化栈:
    9. void StackInit(ST* ps);
    10. //入栈:
    11. void StackPush(ST* ps, STDataType x);
    12. //出栈:
    13. void StackPop(ST* ps);
    14. //获取栈顶的元素:
    15. STDataType StackTop(ST* ps);
    16. //检查栈是否为空:
    17. bool StackEmpty(ST* ps);
    18. //获取栈中有效元素的个数:
    19. int StackSize(ST* ps);
    20. //销毁栈:
    21. void StackDestroy(ST* ps);
    22. void StackInit(ST* ps)
    23. {
    24. assert(ps);
    25. ps->arr = NULL;
    26. ps->top = ps->capacity = 0;
    27. }
    28. void StackPush(ST* ps, STDataType x)
    29. {
    30. assert(ps);
    31. //检查容量:
    32. if (ps->top == ps->capacity)
    33. {
    34. //增容:
    35. int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    36. STDataType* tmp = (STDataType*)realloc(ps->arr, sizeof(STDataType) * newCapacity);
    37. if (tmp == NULL)
    38. {
    39. perror("realloc fail");
    40. exit(-1);
    41. }
    42. ps->arr = tmp;
    43. ps->capacity = newCapacity;
    44. }
    45. ps->arr[ps->top] = x;
    46. ps->top++;
    47. }
    48. bool StackEmpty(ST* ps)
    49. {
    50. assert(ps);
    51. return ps->top == 0;
    52. }
    53. void StackPop(ST* ps)
    54. {
    55. assert(ps);
    56. assert(!StackEmpty(ps));
    57. ps->top--;
    58. }
    59. STDataType StackTop(ST* ps)
    60. {
    61. assert(ps);
    62. assert(!StackEmpty(ps));
    63. return ps->arr[ps->top - 1];
    64. }
    65. int StackSize(ST* ps)
    66. {
    67. assert(ps);
    68. return ps->top;
    69. }
    70. void StackDestroy(ST* ps)
    71. {
    72. assert(ps);
    73. free(ps->arr);
    74. ps->arr = NULL;
    75. ps->capacity = ps->top = 0;
    76. }
    77. bool isValid(char * s){
    78. ST st;
    79. StackInit(&st);
    80. while(*s)
    81. {
    82. //将左括号放入栈中:
    83. if(*s == '(' || *s == '{' || *s == '[')
    84. {
    85. StackPush(&st,*s);
    86. }
    87. else//取出栈中的括号和外边的比较:
    88. {
    89. //取到右括号,栈为空,说明左括号数量不匹配
    90. if(StackEmpty(&st))
    91. {
    92. StackDestroy(&st);
    93. return false;
    94. }
    95. char top = StackTop(&st);
    96. StackPop(&st);
    97. if((*s == '}' && top != '{')
    98. ||(*s == ']' && top != '[')
    99. ||(*s == ')' && top != '('))
    100. {
    101. StackDestroy(&st);
    102. return false;
    103. }
    104. }
    105. s++;
    106. }
    107. //栈不为空说明右括号数量不匹配:
    108. bool flag = StackEmpty(&st);
    109. StackDestroy(&st);
    110. return flag;
    111. }

    题目二:

    用两个队列实现一个栈:oj链接

    解法思路:保持一个队列为空,然后进行转换

    1. typedef int QDataType;
    2. typedef struct QListNode
    3. {
    4. struct QListNode* next;
    5. QDataType data;
    6. }QNode;
    7. typedef struct Queue
    8. {
    9. QNode* head;//头指针
    10. QNode* tail;//尾指针
    11. int size;//记录队列元素个数
    12. }Queue;
    13. //初始化队列:
    14. void QueueInit(Queue* pq);
    15. //销毁队列:
    16. void QueueDestroy(Queue* pq);
    17. //队列插入数据:
    18. void QueuePush(Queue* pq, QDataType x);
    19. //队列删除数据:
    20. void QueuePop(Queue* pq);
    21. //判断队列是否为空:
    22. bool QueueEmpty(Queue* pq);
    23. //获取队列头部的元素:
    24. QDataType QueueFront(Queue* pq);
    25. //获取队列尾部的元素:
    26. QDataType QueueBack(Queue* pq);
    27. //获取队列有效元素个数:
    28. int QueueSize(Queue* pq);
    29. void QueueInit(Queue* pq)
    30. {
    31. assert(pq);
    32. pq->head = pq->tail = NULL;
    33. pq->size = 0;
    34. }
    35. void QueueDestroy(Queue* pq)
    36. {
    37. assert(pq);
    38. QNode* cur = pq->head;
    39. while (cur)
    40. {
    41. QNode* del = cur;
    42. cur = cur->next;
    43. free(del);
    44. del = NULL;
    45. }
    46. pq->head = NULL;
    47. pq->tail = NULL;
    48. }
    49. void QueuePush(Queue* pq, QDataType x)
    50. {
    51. assert(pq);
    52. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    53. if (newnode == NULL)
    54. {
    55. perror("malloc fail:");
    56. exit(-1);
    57. }
    58. else
    59. {
    60. newnode->next = NULL;
    61. newnode->data = x;
    62. }
    63. if (pq->tail == NULL)
    64. {
    65. pq->head = pq->tail = newnode;
    66. }
    67. else
    68. {
    69. pq->tail->next = newnode;
    70. pq->tail = newnode;
    71. }
    72. pq->size++;
    73. }
    74. bool QueueEmpty(Queue* pq)
    75. {
    76. assert(pq);
    77. return pq->head == NULL && pq->tail == NULL;
    78. }
    79. void QueuePop(Queue* pq)
    80. {
    81. assert(pq);
    82. assert(!QueueEmpty(pq));
    83. if (pq->head->next == NULL)
    84. {
    85. free(pq->head);
    86. pq->head = pq->tail = NULL;
    87. }
    88. else
    89. {
    90. Queue* del = pq->head;
    91. pq->head = pq->head->next;
    92. free(del);
    93. del = NULL;
    94. }
    95. pq->size--;
    96. }
    97. QDataType QueueFront(Queue* pq)
    98. {
    99. assert(pq);
    100. assert(!QueueEmpty(pq));
    101. return pq->head->data;
    102. }
    103. QDataType QueueBack(Queue* pq)
    104. {
    105. assert(pq);
    106. assert(!QueueEmpty(pq));
    107. return pq->tail->data;
    108. }
    109. int QueueSize(Queue* pq)
    110. {
    111. return pq->size;
    112. }
    113. typedef struct {
    114. Queue q1;
    115. Queue q2;
    116. } MyStack;
    117. MyStack* myStackCreate() {
    118. MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    119. QueueInit(&obj->q1);
    120. QueueInit(&obj->q2);
    121. return obj;
    122. }
    123. void myStackPush(MyStack* obj, int x) {
    124. if(!QueueEmpty(&obj->q1))
    125. QueuePush(&obj->q1,x);
    126. else
    127. QueuePush(&obj->q2,x);
    128. }
    129. int myStackPop(MyStack* obj) {
    130. Queue* empty = &obj->q1;
    131. Queue* noEmpty = &obj->q2;
    132. if(!QueueEmpty(&obj->q1))
    133. {
    134. empty = &obj->q2;
    135. noEmpty = &obj->q1;
    136. }
    137. while(QueueSize(noEmpty) > 1)
    138. {
    139. QueuePush(empty,QueueFront(noEmpty));
    140. QueuePop(noEmpty);
    141. }
    142. int top = QueueFront(noEmpty);
    143. QueuePop(noEmpty);
    144. return top;
    145. }
    146. int myStackTop(MyStack* obj) {
    147. if(!QueueEmpty(&obj->q1))
    148. return QueueBack(&obj->q1);
    149. else
    150. return QueueBack(&obj->q2);
    151. }
    152. bool myStackEmpty(MyStack* obj) {
    153. return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
    154. }
    155. void myStackFree(MyStack* obj) {
    156. QueueDestroy(&obj->q1);
    157. QueueDestroy(&obj->q2);
    158. free(obj);
    159. }

    题目三:

    用栈实现队列:oj链接

    解法思路:一个栈中专门用来存放数据,一个栈中专门用来删除数据

     

    1. typedef int STDataType;
    2. typedef struct Stack
    3. {
    4. STDataType* arr;
    5. int top;
    6. int capacity;//记录数组的容量
    7. }ST;
    8. //初始化栈:
    9. void StackInit(ST* ps);
    10. //入栈:
    11. void StackPush(ST* ps, STDataType x);
    12. //出栈:
    13. void StackPop(ST* ps);
    14. //获取栈顶的元素:
    15. STDataType StackTop(ST* ps);
    16. //检查栈是否为空:
    17. bool StackEmpty(ST* ps);
    18. //获取栈中有效元素的个数:
    19. int StackSize(ST* ps);
    20. //销毁栈:
    21. void StackDestroy(ST* ps);
    22. void StackInit(ST* ps)
    23. {
    24. assert(ps);
    25. ps->arr = NULL;
    26. ps->top = ps->capacity = 0;
    27. }
    28. void StackPush(ST* ps, STDataType x)
    29. {
    30. assert(ps);
    31. //检查容量:
    32. if (ps->top == ps->capacity)
    33. {
    34. //增容:
    35. int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    36. STDataType* tmp = (STDataType*)realloc(ps->arr, sizeof(STDataType) * newCapacity);
    37. if (tmp == NULL)
    38. {
    39. perror("realloc fail");
    40. exit(-1);
    41. }
    42. ps->arr = tmp;
    43. ps->capacity = newCapacity;
    44. }
    45. ps->arr[ps->top] = x;
    46. ps->top++;
    47. }
    48. bool StackEmpty(ST* ps)
    49. {
    50. assert(ps);
    51. return ps->top == 0;
    52. }
    53. void StackPop(ST* ps)
    54. {
    55. assert(ps);
    56. assert(!StackEmpty(ps));
    57. ps->top--;
    58. }
    59. STDataType StackTop(ST* ps)
    60. {
    61. assert(ps);
    62. assert(!StackEmpty(ps));
    63. return ps->arr[ps->top - 1];
    64. }
    65. int StackSize(ST* ps)
    66. {
    67. assert(ps);
    68. return ps->top;
    69. }
    70. void StackDestroy(ST* ps)
    71. {
    72. assert(ps);
    73. free(ps->arr);
    74. ps->arr = NULL;
    75. ps->capacity = ps->top = 0;
    76. }
    77. typedef struct {
    78. ST pushST;
    79. ST popST;
    80. } MyQueue;
    81. MyQueue* myQueueCreate() {
    82. MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    83. StackInit(&obj->pushST);
    84. StackInit(&obj->popST);
    85. return obj;
    86. }
    87. void myQueuePush(MyQueue* obj, int x) {
    88. StackPush(&obj->pushST,x);
    89. }
    90. void pushSTTopopST(MyQueue* obj)
    91. {
    92. if(StackEmpty(&obj->popST))
    93. {
    94. while(!StackEmpty(&obj->pushST))
    95. {
    96. StackPush(&obj->popST,StackTop(&obj->pushST));
    97. StackPop(&obj->pushST);
    98. }
    99. }
    100. }
    101. int myQueuePop(MyQueue* obj) {
    102. pushSTTopopST(obj);
    103. int front = StackTop(&obj->popST);
    104. StackPop(&obj->popST);
    105. return front;
    106. }
    107. int myQueuePeek(MyQueue* obj) {
    108. pushSTTopopST(obj);
    109. return StackTop(&obj->popST);
    110. }
    111. bool myQueueEmpty(MyQueue* obj) {
    112. return StackEmpty(&obj->pushST) && StackEmpty(&obj->popST);
    113. }
    114. void myQueueFree(MyQueue* obj) {
    115. StackDestroy(&obj->pushST);
    116. StackDestroy(&obj->popST);
    117. free(obj);
    118. }

    题目四:

    设计循环队列:oj链接

    解法思路:用数组实现

    循环队列的逻辑结构:

    定义两个下标依次插入数据,为了区分数据空和满的情况,需要多开辟一个空间,当back的下一个位置等于front的时候说明队列数据满了:

    1. typedef struct {
    2. int* a;
    3. int front;//标记头
    4. int back;//标记尾
    5. int N;//记录空间大小
    6. } MyCircularQueue;
    7. MyCircularQueue* myCircularQueueCreate(int k) {
    8. MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    9. obj->a = (int*)malloc(sizeof(int)*(k+1));//多开辟一个空间
    10. obj->front = 0;
    11. obj->back = 0;
    12. obj->N = k + 1;
    13. return obj;
    14. }
    15. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    16. return obj->front == obj->back;
    17. }
    18. bool myCircularQueueIsFull(MyCircularQueue* obj) {
    19. //%obj->N 当back在最后的时候回到起始位置
    20. return (obj->back + 1) % obj->N == obj->front;
    21. }
    22. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    23. if(myCircularQueueIsFull(obj))
    24. return false;
    25. obj->a[obj->back] = value;
    26. obj->back++;
    27. //当到队尾的时候,返回到起始位置
    28. obj->back %= obj->N;
    29. return true;
    30. }
    31. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    32. if(myCircularQueueIsEmpty(obj))
    33. return false;
    34. obj->front++;
    35. // //当到队尾的时候,返回到起始位置
    36. obj->front %= obj->N;
    37. return true;
    38. }
    39. int myCircularQueueFront(MyCircularQueue* obj) {
    40. if(myCircularQueueIsEmpty(obj))
    41. return -1;
    42. return obj->a[obj->front];
    43. }
    44. int myCircularQueueRear(MyCircularQueue* obj) {
    45. if(myCircularQueueIsEmpty(obj))
    46. return -1;
    47. else if(obj->back == 0)
    48. return obj->a[obj->N-1];
    49. else
    50. return obj->a[obj->back-1];
    51. }
    52. void myCircularQueueFree(MyCircularQueue* obj) {
    53. free(obj->a);
    54. obj->a = NULL;
    55. free(obj);
    56. obj = NULL;
    57. }

    总结:

    以上就是关于栈和队列的知识点和相关面试题,本质上栈和队列也是在内存中管理数据,是通过数组和链表的结构来实现的。

  • 相关阅读:
    利用PHP的特性做免杀Webshell
    Linux内核驱动开发的需要掌握的知识点
    〔003〕虚幻 UE5 基础教程和蓝图入门
    java学习(常用类)
    如何使用Docker安装最新版本的Redis并设置远程访问(含免费可视化工具)
    Nginx下PHP连接到GBase 8s数据库 - PDO_GBASEDBT方式
    【牛客刷题】每日一练—ArrayList的实例强化
    进程调度算法详解
    头哥实践平台之Linux 文件/目录管理
    bind搭建内网DNS服务器架构(主从、子域授权、DNS转发器)
  • 原文地址:https://blog.csdn.net/qq_65307907/article/details/126166669