当前位置: 首页 > news >正文

栈和队列相关|有效的括号|用队列实现栈|用栈实现队列|设计循环队列(C)

20. 有效的括号


判断左右括号是否匹配,匹配返回true,不匹配返回false
通过栈来实现,类型和顺序,数量都要匹配
控制数量通过size
每个右括号都要找最近的左括号去判断类型匹配不匹配,顺序匹配不匹配
最后来判断数量匹配不匹配

  1. 左括号,入栈
  2. 右括号,出栈顶括号,进行匹配
栈接口
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef char STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;void STInit(ST* ps)
{assert(ps);ps->a = NULL;ps->capacity = 0;ps->top = 0;
}void STDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}void STPush(ST* ps, STDataType x)
{assert(ps);// 11:40if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}void STPop(ST* ps)
{assert(ps);// assert(ps->top > 0);--ps->top;
}STDataType STTop(ST* ps)
{assert(ps);// assert(ps->top > 0);return ps->a[ps->top - 1];
}int STSize(ST* ps)
{assert(ps);return ps->top;
}bool STEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
if版本

bool isValid(char * s){ST st;STInit(&st);char top;while(*s){if (*s == '[' || *s == '(' || *s == '{'){STPush(&st, *s);}else{//数量不匹配if (STEmpty(&st)){STDestroy(&st);return false;}top = STTop(&st);STPop(&st);//顺序不匹配if((*s == ']' && top != '[')|| (*s == ')' && top != '(')|| (*s == '}' && top != '{')){STDestroy(&st);return false;}}++s;}// 栈不为空,false,说明数量不匹配bool ret = STEmpty(&st);STDestroy(&st);return ret;
}
switch版本
bool isValid(char * s){ST st;STInit(&st);char top;while (*s){switch(*s){case '[':case '(':case '{':STPush(&st, *s);break;case '}':// 数量不匹配if (STEmpty(&st))return false;top = STTop(&st);STPop(&st);// 顺序不匹配if (top != '{')return false;break;case ']':// 数量不匹配if (STEmpty(&st))return false;top = STTop(&st);STPop(&st);//顺序不匹配if (top != '[')return false;break;case ')':// 数量不匹配if (STEmpty(&st))return false;top = STTop(&st);STPop(&st);//顺序不匹配if (top != '(')return false;break;}++s;}//数量不匹配bool ret = STEmpty(&st);STDestroy(&st);return ret;
}

225. 用队列实现栈


用两个队列实现栈

Push 1 2 3 4
![[Pasted image 20241030145528.png]]

Pop 4
分别出1 2 3,插入到另一个队列,最后剩下4,把最后一个数据Pop掉
![[Pasted image 20241030150220.png]]

![[Pasted image 20241030150525.png]]

Push 5 6
入到不为空的那个队列里
![[Pasted image 20241030150637.png]]

空队列是用来倒数据的

Pop 6
![[Pasted image 20241030151026.png]]

入队列:不为空的队列
出队列:不为空队列前N-1个出队列,插入空队列;删除剩余数据

队列接口
typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* head;QNode* tail;int size;
}Que;void QueueInit(Que* pq);
void QueueDestroy(Que* pq);
void QueuePush(Que* pq, QDataType x);
void QueuePop(Que* pq);
QDataType QueueFront(Que* pq);
QDataType QueueBack(Que* pq);
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);void QueueInit(Que* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}void QueueDestroy(Que* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->head = pq->tail = NULL;pq->size = 0;
}void QueuePush(Que* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}newnode->data = x;newnode->next = NULL;if (pq->tail == NULL){pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}pq->size++;
}void QueuePop(Que* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* next = pq->head->next;free(pq->head);pq->head = next;}pq->size--;
}QDataType QueueFront(Que* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}QDataType QueueBack(Que* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}bool QueueEmpty(Que* pq)
{assert(pq);return pq->head == NULL;
}int QueueSize(Que* pq)
{assert(pq);return pq->size;
}
队列实现栈

![[Pasted image 20241030160553.png]]

typedef struct {Que q1;Que q2;
} MyStack;MyStack* myStackCreate() {MyStack* pst = (MyStack*)malloc(sizeof(MyStack));QueueInit(&pst->q1);QueueInit(&pst->q2);return pst;
}void myStackPush(MyStack* obj, int x) {if (!QueueEmpty(&obj->q1)){QueuePush(&obj->q1, x);}else{QueuePush(&obj->q2, x);}
}int myStackPop(MyStack* obj) {Que* empty = &obj->q1;Que* nonempty = &obj->q2;if (!QueueEmpty(&obj->q1)){nonempty = &obj->q1;empty = &obj->q2;}while(QueueSize(nonempty) > 1){// 前size-1个导入空队列QueuePush(empty, QueueFront(nonempty));QueuePop(nonempty);}int top = QueueFront(nonempty);QueuePop(nonempty);return top;
}int myStackTop(MyStack* obj) {if (!QueueEmpty(&obj->q1)){return QueueBack(&obj->q1);}else{return QueueBack(&obj->q2);}
}bool myStackEmpty(MyStack* obj) {return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}void myStackFree(MyStack* obj) {QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);
}

232. 用栈实现队列


用两个栈来实现队列
Push 1 2 3 4
![[Pasted image 20241030185925.png]]

Pop 1
把数据往另一个栈里倒
![[Pasted image 20241030190053.png]]

Push 5 6
![[Pasted image 20241030190441.png]]

存数据的栈称为pushst,出数据的栈称为popst

栈接口
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;void STInit(ST* ps)
{assert(ps);ps->a = NULL;ps->capacity = 0;ps->top = 0;
}void STDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}void STPush(ST* ps, STDataType x)
{assert(ps);// 11:40if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}void STPop(ST* ps)
{assert(ps);// assert(ps->top > 0);--ps->top;
}STDataType STTop(ST* ps)
{assert(ps);// assert(ps->top > 0);return ps->a[ps->top - 1];
}int STSize(ST* ps)
{assert(ps);return ps->top;
}bool STEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
栈实现队列

typedef struct {ST pushst;ST popst;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));STInit(&obj->pushst);STInit(&obj->popst);return obj;
}void myQueuePush(MyQueue* obj, int x) {STPush(&obj->pushst, x);
}int myQueuePeek(MyQueue* obj) {if(STEmpty(&obj->popst)){// 倒数据while(!STEmpty(&obj->pushst)){STPush(&obj->popst, STTop(&obj->pushst));STPop(&obj->pushst);}}return STTop(&obj->popst);
}int myQueuePop(MyQueue* obj) {int front = myQueuePeek(obj);STPop(&obj->popst);return front;
}bool myQueueEmpty(MyQueue* obj) {return STEmpty(&obj->popst) && STEmpty(&obj->pushst);
}void myQueueFree(MyQueue* obj) {STDestroy(&obj->popst);STDestroy(&obj->pushst);free(obj);
}

622. 设计循环队列


![[Pasted image 20241030200421.png]]

循环队列
Pop 1
![[Pasted image 20241030200445.png]]

Push 5
在这里插入图片描述

Pop 2
![[Pasted image 20241030200620.png]]

Push 6
![[Pasted image 20241030200639.png]]

当队列为空
![[Pasted image 20241030200914.png]]

front和rear相等,队列为空
插入一个数据,rear往后走
rear不是指向最后一个数据,而是指向最后一个数据的下一个位置

如何判断队列满

多开一个空间
k == 4,表示队列只能存四个数
![[Pasted image 20241030202645.png]]

front == rear 空
rear的下一个就是front 满

  • rear在中间的情况
    ![[Pasted image 20241030205436.png]]
(rear + 1)% (k + 1) == front
取队尾

![[Pasted image 20241030212834.png]]

![[Pasted image 20241030212743.png]]

(rear + (k + 1) - 1) % (k + 1)

循环队列实现
typedef struct {int* a;int front;int rear;int k;
} MyCircularQueue;MyCircularQueue* myCircularQueueCreate(int k) {MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));// 多开一个方便区分空和满obj->a = (int*)malloc(sizeof(int)*(k+1));obj->front = obj->rear = 0;obj->k = k;return obj;
}bool myCircularQueueIsEmpty(MyCircularQueue* obj) {return obj->front == obj->rear;
}bool myCircularQueueIsFull(MyCircularQueue* obj) {return (obj->rear + 1)%(obj->k + 1) == obj->front;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {if(myCircularQueueIsFull(obj))return false;obj->a[obj->rear] = value;obj->rear++;obj->rear %= (obj->k + 1);return true;
}bool myCircularQueueDeQueue(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj))return false;++obj->front;obj->front %= (obj->k + 1);return true;
}int myCircularQueueFront(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj))return -1;elsereturn obj->a[obj->front];
}int myCircularQueueRear(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj))return -1;else//(rear + (k + 1) - 1) % (k + 1)return obj->a[(obj->rear + obj->k) % (obj->k + 1)];
}void myCircularQueueFree(MyCircularQueue* obj) {free(obj->a);free(obj);
}
http://www.lryc.cn/news/472880.html

相关文章:

  • 云原生后端开发教程
  • TortoiseSVN小乌龟下载安装(Windows11)
  • Android adb命令获取设备id
  • Skywalking教程一
  • React中管理state的方式
  • 服务器数据恢复—RAID5阵列中部分成员盘重组RAID5阵列后如何恢复原raid5阵列数据?
  • 【Linux】文件切割排序 cut sort
  • 零售EDI:HornBach EDI 项目案例
  • SpringBoot 集成RabbitMQ 实现钉钉日报定时发送功能
  • 基于java ssm springboot女士电商平台系统源码+文档设计
  • Matlab数字信号处理——基于改进小波变换的图像去噪方法(7种去噪算法)
  • leetcode hot100【LeetCode 70. 爬楼梯】java实现
  • Java异常2
  • 2024熵密杯初始题2
  • echarts属性之title
  • VUE errolog, vue 错误集
  • 驱动开发系列13 - Linux tasklet用法介绍
  • redis实现分布式锁,go实现完整code
  • 解析日期、编码
  • 【Qt】QApplication::restoreOverrideCursor():恢复鼠标光标到原始状态的用法解析
  • 重生之“我打数据结构,真的假的?”--2.单链表(无习题)
  • 【有啥问啥】视频插帧算法技术原理详解
  • Leetcode148,109以及二者的合并 -> Tencent面试算法题 - 无序双向链表转BST
  • 【蓝桥杯选拔赛真题77】python计算小球 第十五届青少年组蓝桥杯python选拔赛真题 算法思维真题解析
  • 获取Hive表备注
  • 10.30学习
  • 什么是栈溢出
  • 在linux中arm-linux-gcc和/usr/bin/gcc有啥区别
  • 常用环境部署(二十二)——MySQL的数据库迁移到另一个机器上
  • 两台主机只能单方向ping通