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

临沂小学网站建设浏览器正能量网站免费

临沂小学网站建设,浏览器正能量网站免费,it培训费用大概多少钱,乌鲁木齐网页制作目录 一 用队列实现栈 二 用栈实现队列 三 设计循环队列 四 有效的括号 一 用队列实现栈 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.fp688.cn/news/1209.html

相关文章:

  • 一区适合晚上一个人看b站长沙百度快速排名
  • 传媒网站建设惠州seo外包
  • 建设银行官网网站首页站长工具国产
  • 自己的ip做网站广州网站关键词排名
  • 一级页面的网站怎么做企拓客app骗局
  • 如何做外贸网站推广seo网络推广是干嘛的
  • wordpress没权限关键词优化是什么意思
  • 精彩网站制作浏览器下载
  • 大连企业建站程序品牌策划是做什么的
  • 网站建设免费软件百度搜索关键词统计
  • 学做网站能赚多少上海知名的seo推广咨询
  • 邵阳市住房和城乡建设局网站百度 营销怎么收费
  • 网站建设合同标准版福州短视频seo机会
  • 高端网站定做如何自己做网页
  • wordpress远程提升seo排名平台
  • 做的网站访问不了免费浏览网站推广
  • 连云港网站建设 连云港网站制作查网站
  • 做一个京东网站怎么做的百度发作品入口在哪里
  • 购物帮 做特惠的导购网站优化培训学校
  • 扬州鼎盛开发建设有限公司网站抖音关键词排名软件
  • 温州哪里有网站建设seo专员是什么
  • 郴州建设网站制作seo 优化 工具
  • 政务公开和网站建设情况百度推广代理怎么加盟
  • 网站搜索排名站长工具在线免费
  • 泰安定制网站建设公司最近一周国内热点新闻
  • 深圳网站建设_企业网站设计定制百度手机app下载安装
  • 厦门网站建设哪家强网络营销的核心是
  • html5动态效果的网站是怎么做的百度手机浏览器下载
  • 网站你懂我意思正能量晚上在线观看不用下载免费苹果seo推广多少钱
  • wordpress获取当前目录父目录idseo外链推广工具