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

自建电商网站销售商品wordpress注册简化

自建电商网站销售商品,wordpress注册简化,手机h5免费模板网站,冷链物流20. 有效的括号 判断左右括号是否匹配,匹配返回true,不匹配返回false 通过栈来实现,类型和顺序,数量都要匹配 控制数量通过size 每个右括号都要找最近的左括号去判断类型匹配不匹配,顺序匹配不匹配 最后来判断数量匹配…

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.yayakq.cn/news/409492/

相关文章:

  • 阅读网站源码三乡网站建设公司
  • 建设一个公司网站大概多少钱如何策划手机网站
  • 企业网站帮助中心ui设计参考网站
  • 南华大学城市建设学院网站新加坡网站域名
  • html 如何嵌入网站页面荥阳市城乡规划和建设局网站
  • 建设网站好公司哪家好室内平面设计软件
  • 杭州网站建设h5广告平面设计基础知识
  • 设计网站如何融入非关系数据库163企业邮箱费用多少
  • 网站建设流程有wordpress ico不显示
  • 企业营销型网站建设方案购买一个网站域名需要多少钱
  • 高港做网站红河州seo怎么做整站排名
  • qq免费搭建网站意见反馈的网站怎么做
  • 巴里坤网站建设省建设干部培训中心网站
  • 西安竞价推广托管360优化大师旧版本
  • 智能科技 光速东莞网站建设自己怎样建设网站首页
  • 做网站需要到哪些部门登记京东企业集团网站建设方案
  • 昌邑建设网站html页面添加wordpress
  • 网站建设的费用是不是含税的长沙个人做网站排名
  • 如何建设微商网站网站备案查询
  • 免费网站建设绑定域名素材网有哪些
  • 四川省城乡住房与建设厅网站免费微网站怎么做
  • 广州网站建设50强名单渭南网站制作学校
  • 网站后台用什么程序做门户设计方案
  • 自行建造网站深圳网站制作公司兴田德润信任高
  • 优秀设计网站大全网络工具
  • 做游戏视频网站电商网站运营建设的目标
  • 中国最早做网站是谁中国建设厅官方网站
  • 机械网站开发方案海口紧急通告
  • 惠东县住房和城乡规划建设局网站网站移动窗口代码
  • 常州网站建设专业的公司济南建设网点电话