关键词搜索

源码搜索 ×
×

【数据结构】栈和队列典例--纯C

发布2022-04-06浏览1756次

详情内容

 s​​​​​​​20. 有效的括号

通过题目描述的问题,使用栈来解决是最优解。

思路:1)找到左括号们,将其入栈,遇到右括号就开始依次出栈,如果不符合匹配标准,就返回false

2)匹配过程中,会出现栈内数据为空但是需要从站内取数据的情况,出现遍历字符串结束后,栈内数据不为空的情况都需要额外考虑。

画图:

针对思路2的要额外处理

代码:

  1. typedef int STDataType;
  2. typedef struct Stack
  3. {
  4. STDataType* a;
  5. int top;//栈顶
  6. int capacity;//容量
  7. }Stack;
  8. void StackInit(Stack* ps)
  9. {
  10. ps->a = NULL;
  11. ps->capacity = ps->top = 0;
  12. }
  13. //销毁
  14. void StackDestroy(Stack* ps)
  15. {
  16. free(ps->a);
  17. ps->a = NULL;
  18. ps->capacity = ps->top = 0;
  19. }
  20. //入栈
  21. void StackPush(Stack* ps, STDataType x)
  22. {
  23. //断言
  24. assert(ps);
  25. //判断是否需要扩容
  26. if (ps->top == ps->capacity)
  27. {
  28. int newcapacity = ps->capacity == 0 ? ps->capacity = 4 : ps->capacity * 2;
  29. STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
  30. //判断扩容是否成功
  31. if (tmp == NULL)
  32. {
  33. printf("realloc fail\n");
  34. exit(-1);
  35. }
  36. ps->a = tmp;
  37. ps->capacity = newcapacity;
  38. }
  39. //插入数据
  40. //数组的下标从0开始,top指向的就是栈顶
  41. ps->a[ps->top] = x;
  42. ps->top++;
  43. }
  44. //出栈
  45. void StackPop(Stack* ps)
  46. {
  47. assert(ps);
  48. //断言栈是否为空
  49. assert(ps->top > 0);
  50. ps->top--;
  51. }
  52. //获取栈顶元素
  53. STDataType StackTop(Stack* ps)
  54. {
  55. assert(ps);
  56. //断言栈是否为空
  57. assert(ps->top);
  58. return ps->a[ps->top-1];
  59. }
  60. //检测栈是否为空
  61. bool StackEmpty(Stack* ps)
  62. {
  63. assert(ps);
  64. 非空
  65. //if (ps->top > 0)
  66. //{
  67. // return false;
  68. //}
  69. 为空
  70. //else
  71. // return true;
  72. //更优化的写法
  73. return ps->top == 0;
  74. }
  75. int StackSize(Stack* ps)
  76. {
  77. assert(ps);
  78. //返回个数,top指的是栈顶数据的下一位。
  79. return ps->top;
  80. }
  81. bool isValid(char * s){
  82. Stack st;
  83. StackInit(&st);//初始化
  84. while(*s!='\0')//遍历
  85. {
  86. if(*s=='{'||*s=='['||*s=='(')
  87. {
  88. StackPush(&st,*s);//当为{,[,( 时入栈
  89. s++;//更新
  90. }
  91. else
  92. {
  93. if(!StackEmpty(&st))//当栈不为空时
  94. {
  95. char top = StackTop(&st);//取出栈顶数据
  96. StackPop(&st);//删除栈顶数据
  97. if((top == '{'&& *s!='}')||(top == '('&&*s!=')'||top == '['&&*s!=']'))//如果不匹配,就返回false
  98. {
  99. return false;
  100. }
  101. else
  102. {
  103. s++;
  104. }
  105. }
  106. else//当栈为空时,说明其缺少左括号,必然不能配对
  107. return false;
  108. }
  109. }
  110. //循环结束时
  111. int ret = StackEmpty(&st);//记录栈是否为空
  112. StackDestroy(&st);//销毁栈
  113. if(ret)//当栈为空,即结束了匹配
  114. return true;
  115. else//栈内有数据,就说明循环结束时,原字符串有单个左括号无法被匹配。
  116. return false;
  117. }

225. 用队列实现栈

思路:队列的性质是:先入先出;栈的性质是:后入后出。

可以创建两个队列,先使用一个存数据,要取出数据时,可以将前N-1个数据移到另一个队列,取出(并删除)数据,以此往复,模拟实现栈的相关功能。

画图:

移除并返回栈顶元素:

取出5

将队列1中的数据清空

插入数据:

刚开始可以随意选择一个空的队列插入,在后面插入的元素都必须插入到非空队列中。

这里可以通过选择语句确立空与非空队列。

返回栈顶元素:

队列拥有访问队尾元素的功能,所以可以直接通过队列来实现。

判断是否为空:

队列1和 队列2 同时为空时,栈为空。

代码:

  1. typedef int QDataType;
  2. //链表的节点
  3. typedef struct QueueNode
  4. {
  5. QDataType data;//数据
  6. struct QueueNode* next;//标记下一个节点
  7. }QNode;
  8. typedef struct Queue
  9. {
  10. QNode* head;//头指针
  11. QNode* tail;//尾指针
  12. }Queue;
  13. void QueueInit(Queue* pq)
  14. {
  15. assert(pq);
  16. pq->head = pq->tail = NULL;
  17. }
  18. void QueueDestory(Queue* pq)
  19. {
  20. assert(pq);
  21. QNode* cur = pq->head;
  22. while (cur)
  23. {
  24. QNode* next = cur->next;
  25. free(cur);
  26. cur = next;
  27. }
  28. pq->head = pq->tail = NULL;
  29. }
  30. void QueuePush(Queue* pq, QDataType x)
  31. {
  32. assert(pq);
  33. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  34. assert(newnode);
  35. newnode->data = x;
  36. newnode->next = NULL;
  37. if (pq->tail == NULL)
  38. {
  39. assert(pq->head == NULL);
  40. pq->head = pq->tail = newnode;
  41. }
  42. else
  43. {
  44. pq->tail->next = newnode;
  45. pq->tail = newnode;
  46. }
  47. }
  48. void QueuePop(Queue* pq)
  49. {
  50. assert(pq);
  51. assert(pq->head && pq->tail);
  52. if (pq->head->next == NULL)
  53. {
  54. free(pq->head);
  55. pq->head = pq->tail = NULL;
  56. }
  57. else
  58. {
  59. QNode* next = pq->head->next;
  60. free(pq->head);
  61. pq->head = next;
  62. }
  63. }
  64. bool QueueEmpty(Queue* pq)
  65. {
  66. assert(pq);
  67. //return pq->head == NULL && pq->tail == NULL;
  68. return pq->head == NULL;
  69. }
  70. size_t QueueSize(Queue* pq)
  71. {
  72. assert(pq);
  73. QNode* cur = pq->head;
  74. size_t size = 0;
  75. while (cur)
  76. {
  77. size++;
  78. cur = cur->next;
  79. }
  80. return size;
  81. }
  82. QDataType QueueFront(Queue* pq)
  83. {
  84. assert(pq);
  85. assert(pq->head);
  86. return pq->head->data;
  87. }
  88. QDataType QueueBack(Queue* pq)
  89. {
  90. assert(pq);
  91. assert(pq->tail);
  92. return pq->tail->data;
  93. }
  94. typedef struct {
  95. //创建两个队列
  96. Queue q1;
  97. Queue q2;
  98. } MyStack;
  99. MyStack* myStackCreate() {
  100. //为结构体分配空间,不能使用
  101. //MyStack st; 栈上开辟的临时变量,出栈即销毁,返回的指针为空指针。
  102. //在堆上开辟空间,malloc动态开辟
  103. MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
  104. //assert(pst);//可有可无
  105. //Mystack结构体成员初始化
  106. QueueInit(&pst->q1);
  107. QueueInit(&pst->q2);
  108. return pst;
  109. }
  110. void myStackPush(MyStack* obj, int x) {
  111. //非空的队列放入数据,始终保持存在一个空的队列
  112. if(!QueueEmpty(&obj->q1))//非空
  113. {
  114. QueuePush(&obj->q1,x);
  115. }
  116. else
  117. {
  118. QueuePush(&obj->q2,x);
  119. }
  120. }
  121. int myStackPop(MyStack* obj) {
  122. //assert(obj);
  123. //指定一个空队列和非空队列,定义q1为空
  124. Queue* empty = &obj->q1;
  125. Queue* nonempty = &obj->q2;
  126. //若不是,就交换
  127. if(!QueueEmpty(&obj->q1))
  128. {
  129. empty = &obj->q2;
  130. nonempty = &obj->q1;
  131. }
  132. while(QueueSize(nonempty)>1)//从非空队列中持续取数据放入空队列中,并删数据
  133. {
  134. QDataType front = QueueFront(nonempty);//获取数据
  135. QueuePush(empty,front);//插入数据
  136. QueuePop(nonempty);//删数据
  137. }
  138. //获取“栈顶”数据
  139. QDataType top = QueueFront(nonempty);
  140. 移除栈顶数据
  141. QueuePop(nonempty);
  142. //返回“栈顶”元素
  143. return top;
  144. }
  145. int myStackTop(MyStack* obj) {
  146. //指定一个空队列和非空队列,定义q1为空
  147. Queue* empty = &obj->q1;
  148. Queue* nonempty = &obj->q2;
  149. //若不是,就交换
  150. if(!QueueEmpty(&obj->q1))
  151. {
  152. empty = &obj->q2;
  153. nonempty = &obj->q1;
  154. }
  155. return QueueBack(nonempty);
  156. }
  157. bool myStackEmpty(MyStack* obj) {
  158. assert(obj);
  159. //当q1和q2同时为空时,“栈”为空
  160. return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
  161. }
  162. void myStackFree(MyStack* obj) {
  163. //释放内存时,先解决结构体内部的q1和q2
  164. assert(obj);
  165. QueueDestory(&obj->q1);
  166. QueueDestory(&obj->q2);
  167. free(obj);
  168. //置空并不会改变实参的状态
  169. }

用栈实现队列

思路:栈的性质:后入后出;队列的性质:先入先出。

创立两个栈,在两个栈上模拟实现队列的基本功能。

画图:

先入1 2 3 4 5 数据

pop

取出队头元素1

将栈1的所有元素移植至栈2

从栈二中提取目标元素1

peek

通过pop可以看出,栈2中的数据可以模仿队列出数据。

由此可以得出结论:模拟实现队列只需要导数据1次即可。

 

push

 

根据已有的结论,将数据先导入栈1中,等到栈2中的数据被删空时再将栈1中的数据导入栈2中。

 

empty

1 和 栈2 同时为空时队列为空。

 

注意:在pop和 push数据时,要判断是否为空的情况。

 

代码:

  1. typedef int STDataType;
  2. typedef struct Stack
  3. {
  4. STDataType* a;
  5. int top;//栈顶
  6. int capacity;//容量
  7. }Stack;
  8. void StackInit(Stack* ps)
  9. {
  10. ps->a = NULL;
  11. ps->capacity = ps->top = 0;
  12. }
  13. //销毁
  14. void StackDestroy(Stack* ps)
  15. {
  16. free(ps->a);
  17. ps->a = NULL;
  18. ps->capacity = ps->top = 0;
  19. }
  20. //入栈
  21. void StackPush(Stack* ps, STDataType x)
  22. {
  23. //断言
  24. assert(ps);
  25. //判断是否需要扩容
  26. if (ps->top == ps->capacity)
  27. {
  28. int newcapacity = ps->capacity == 0 ? ps->capacity = 4 : ps->capacity * 2;
  29. STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
  30. //判断扩容是否成功
  31. if (tmp == NULL)
  32. {
  33. printf("realloc fail\n");
  34. exit(-1);
  35. }
  36. ps->a = tmp;
  37. ps->capacity = newcapacity;
  38. }
  39. //插入数据
  40. //数组的下标从0开始,top指向的就是栈顶
  41. ps->a[ps->top] = x;
  42. ps->top++;
  43. }
  44. //出栈
  45. void StackPop(Stack* ps)
  46. {
  47. assert(ps);
  48. //断言栈是否为空
  49. assert(ps->top > 0);
  50. ps->top--;
  51. }
  52. //获取栈顶元素
  53. STDataType StackTop(Stack* ps)
  54. {
  55. assert(ps);
  56. //断言栈是否为空
  57. assert(ps->top);
  58. return ps->a[ps->top-1];
  59. }
  60. //检测栈是否为空
  61. bool StackEmpty(Stack* ps)
  62. {
  63. assert(ps);
  64. 非空
  65. //if (ps->top > 0)
  66. //{
  67. // return false;
  68. //}
  69. 为空
  70. //else
  71. // return true;
  72. //更优化的写法
  73. return ps->top == 0;
  74. }
  75. int StackSize(Stack* ps)
  76. {
  77. assert(ps);
  78. //返回个数,top指的是栈顶数据的下一位。
  79. return ps->top;
  80. }
  81. typedef struct {
  82. Stack s1;//储存数据最初始的栈
  83. Stack s2;//取数据专用的栈
  84. } MyQueue;
  85. bool myQueueEmpty(MyQueue* obj);
  86. MyQueue* myQueueCreate() {
  87. //分配空间
  88. MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
  89. //初始化
  90. StackInit(&obj->s1);//放数据
  91. StackInit(&obj->s2);
  92. return obj;
  93. }
  94. void myQueuePush(MyQueue* obj, int x) {
  95. //插入数据,s1为储存初始数据的栈
  96. StackPush(&obj->s1,x);
  97. }
  98. int myQueuePop(MyQueue* obj) {
  99. //如果s2为空
  100. if(StackEmpty(&obj->s2))
  101. {
  102. //导数据
  103. while(StackSize(&obj->s1))
  104. {
  105. int top = StackTop(&obj->s1);
  106. StackPush(&obj->s2,top);
  107. StackPop(&obj->s1);
  108. }
  109. }
  110. int top = StackTop(&obj->s2);
  111. StackPop(&obj->s2);
  112. return top;
  113. }
  114. int myQueuePeek(MyQueue* obj) {
  115. if(StackEmpty(&obj->s2))
  116. {
  117. while(StackSize(&obj->s1))
  118. {
  119. int top = StackTop(&obj->s1);
  120. StackPush(&obj->s2,top);
  121. StackPop(&obj->s1);
  122. }
  123. }
  124. int top = StackTop(&obj->s2);
  125. return top;
  126. }
  127. bool myQueueEmpty(MyQueue* obj) {
  128. return StackEmpty(&obj->s2) && StackEmpty(&obj->s1);
  129. }
  130. void myQueueFree(MyQueue* obj) {
  131. StackDestroy(&obj->s1);
  132. StackDestroy(&obj->s2);
  133. free(obj);
  134. }

622. 设计循环队列

思路:

创建结构体变量,记录头尾,k,指针数组。

判断为空的条件:当head == tail时表示队列为空。

但是队列已满的条件也为head == tail。

解决方案:

为空

为了能够使用head == tail的判定

条件,一般认为这个情况是满的。

为保证空间一定能存放下k个元素,将数组空间+1即可满足。

此时判断为空的条件:head == tail

  判断为满的条件:head = tail+1

注意:多出来的一个空间在数组中时随机的,并不是在数组末尾。

画图:

环形只存5个数据,开辟了6个空间

经过一系列插入,删除操作:

再插入值

此时已经插入了5个值了,队列已经满了,判定条件就成了tail+1 == head

这是多次插入和删除情况下的,若是不进行删除操作:

此时就已经满了。判定条件 tail == k

 

代码:

  1. typedef struct {
  2. //数组,头,尾,k
  3. int* a;
  4. int head;
  5. int tail;
  6. int k;
  7. } MyCircularQueue;
  8. bool myCircularQueueIsEmpty(MyCircularQueue* obj);
  9. bool myCircularQueueIsFull(MyCircularQueue* obj);
  10. MyCircularQueue* myCircularQueueCreate(int k) {
  11. //分配空间
  12. MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
  13. //结构体内部的数组也需要开辟空间,为方便找到循环结束条件多开一个单位
  14. obj->a = (int*)malloc(sizeof(int)*(k+1));
  15. //初始化
  16. obj->head = obj->tail = 0;
  17. obj->k = k;
  18. return obj;
  19. }
  20. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
  21. //判断循环队列是否已满
  22. if(myCircularQueueIsFull(obj))
  23. {
  24. return false;
  25. }
  26. else
  27. {
  28. obj->a[obj->tail] = value;
  29. //更新tail
  30. //如果走到尾的话,就归0
  31. if(obj->tail == obj->k)
  32. {
  33. obj->tail = 0;
  34. }
  35. else
  36. obj->tail++;
  37. }
  38. return true;
  39. }
  40. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
  41. if(myCircularQueueIsEmpty(obj))
  42. {
  43. return false;
  44. }
  45. else
  46. {
  47. //更新头
  48. if(obj->head == obj->k)
  49. {
  50. obj->head = 0;
  51. }
  52. //数据覆盖即可
  53. else
  54. obj->head++;
  55. return true;
  56. }
  57. }
  58. int myCircularQueueFront(MyCircularQueue* obj) {
  59. if(myCircularQueueIsEmpty(obj))
  60. {
  61. return -1;
  62. }
  63. else
  64. {
  65. return obj->a[obj->head];
  66. }
  67. }
  68. int myCircularQueueRear(MyCircularQueue* obj) {
  69. if(myCircularQueueIsEmpty(obj))
  70. {
  71. return -1;
  72. }
  73. else
  74. {
  75. if(obj->tail == 0)
  76. {
  77. return obj->a[obj->k];
  78. }
  79. else
  80. return obj->a[obj->tail-1];
  81. }
  82. }
  83. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
  84. return obj->head == obj->tail;
  85. }
  86. bool myCircularQueueIsFull(MyCircularQueue* obj) {
  87. if(obj->head==0 && obj->tail == obj->k)
  88. {
  89. return true;
  90. }
  91. else if(obj->tail+1 == obj->head)
  92. {
  93. return true;
  94. }
  95. else
  96. return false;
  97. }
  98. void myCircularQueueFree(MyCircularQueue* obj) {
  99. free(obj->a);
  100. free(obj);
  101. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载