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

龙岩网络图书馆注册刷移动端seo软件

龙岩网络图书馆注册,刷移动端seo软件,深圳网站建设 独,福建金融公司网站建设目录 一 用队列实现栈 二 用栈实现队列 三 设计循环队列 四 有效的括号 一 用队列实现栈 225. 用队列实现栈 - 力扣(LeetCode) typedef int QDataType; typedef struct QueueNode {struct QueueNode* next;QDataType data; }QNode;typedef struct …

目录

一 用队列实现栈

二 用栈实现队列 

三 设计循环队列 

四 有效的括号


一 用队列实现栈

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

 

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)
{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++;
}bool QueueEmpty(Que* pq)
{assert(pq);return pq->head == NULL;
}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;
}int QueueSize(Que* pq)
{assert(pq);return pq->size;
}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){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. 用栈实现队列 - 力扣(LeetCode)

 

typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;void STInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = 0;ps->capacity = 0;
}void STDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = 0;ps->top = ps->capacity = 0;
}void STPush(ST* ps, STDataType x)
{assert(ps);if (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 StackPush;ST StackPop;} MyQueue;MyQueue* myQueueCreate() {MyQueue* pqueue = (MyQueue*)malloc(sizeof(MyQueue));STInit(&pqueue->StackPush);STInit(&pqueue->StackPop);return pqueue;
}void myQueuePush(MyQueue* obj, int x) {STPush(&obj->StackPush, x);
}int myQueuePeek(MyQueue* obj) {if (STEmpty(&obj->StackPop)){while (!STEmpty(&obj->StackPush)){STPush(&obj->StackPop, STTop(&obj->StackPush));STPop(&obj->StackPush);}}return STTop(&obj->StackPop);}int myQueuePop(MyQueue* obj) {int front = myQueuePeek(obj);STPop(&obj->StackPop);return front;
}bool myQueueEmpty(MyQueue* obj) {return STEmpty(&obj->StackPush) && STEmpty(&obj->StackPop);
}void myQueueFree(MyQueue* obj) {STDestroy(&obj->StackPush);STDestroy(&obj->StackPop);free(obj);
}

三 设计循环队列 

622. 设计循环队列 - 力扣(LeetCode)

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;}else{return obj->a[obj->front];}}int myCircularQueueRear(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj)){return -1;}else {return obj->a[(obj->rear + obj->k) % (obj->k + 1)];}}void myCircularQueueFree(MyCircularQueue* obj) {free(obj->a);free(obj);
}

 

 

四 有效的括号

20. 有效的括号 - 力扣(LeetCode)

typedef char STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;void STInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 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);if (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;
}bool isValid(char* s) {ST st;STInit(&st);while (*s){if (*s == '(' || *s == '{' || *s == '['){STPush(&st, *s);}else{if (STEmpty(&st)){STDestroy(&st);return false;}char topval = STTop(&st);STPop(&st);if ((*s == ']' && topval != '[') || (*s == ')' && topval != '(') || (*s == '}' && topval != '{')){STDestroy(&st);return false;}}s++;}bool ret = STEmpty(&st);STDestroy(&st);return ret;
}

 

http://www.tj-hxxt.cn/news/17501.html

相关文章:

  • 孝感网站建设xgsh成都seo经理
  • 国外网站兼职做效果图品牌营销公司
  • 能发朋友圈的网站建设语百度seo点击排名优化
  • 用台式机做网站服务器网站制作公司
  • 什么学做网站企业宣传方式
  • 哪个网站的课件做的好百度老年搜索
  • 西安做网站费用人员优化方案怎么写
  • 烟台市住房和规划建设管理局网站惠州网站建设方案推广
  • 河海大学学风建设网站广州关于进一步优化疫情防控措施
  • 兼职做页面的网站最近的新闻有哪些
  • 公众号推文制作网站沈阳网站制作优化推广
  • 如何用天地图做网站百度竞价返点一般多少
  • 网站空间和云主机seo信息是什么
  • 网页设计网站怎么放到域名里郑州seo优化哪家好
  • 专业网站建设价格大全怎么推广自己的店铺
  • 网站开发难不难东莞网络推广培训
  • 山东外贸国际网站建设如何进行搜索引擎优化 简答案
  • seo网站设计阿里云免费域名
  • 无锡网站开发平台百度云盘登录电脑版
  • 怎么自己做网站赚钱目前在哪个平台做推广好
  • 广西南宁电商网站建设谷歌浏览器手机版官网下载
  • 做染料的网站中国最新军事新闻
  • 建设网站能挣钱吗网站查询ip地址查询
  • 短链生成网站seo营销技巧培训班
  • 做细分领域的同城网站靠谱seo整站优化外包
  • 买目录做网站竞彩足球最新比赛
  • dwcc怎么做网站网站开发技术有哪些
  • 西青做网站网站制作免费
  • 医院网站制作公司重庆网站seo外包
  • 个人备案可以做哪些网站网站seo课设