wordpress前端登录按钮,网站建设seoppt,微信小程序开发实战,公司的网站难不难做目录
栈的实现
1、数组栈
2、链式栈
栈的创建
栈的打印
内存泄漏
栈溢出
练习
有效的括号 栈的实现
栈后入先出
1、数组栈
#xff08;最佳实现#xff0c;且访问数据的时候CPU告诉访存命中率比较高#xff0c;因为地址连续存放#xff0c;访问时CPU从cache里一…目录
栈的实现
1、数组栈
2、链式栈
栈的创建
栈的打印
内存泄漏
栈溢出
练习
有效的括号 栈的实现
栈后入先出
1、数组栈
最佳实现且访问数据的时候CPU告诉访存命中率比较高因为地址连续存放访问时CPU从cache里一次访问一片
数组适合尾插尾删所以栈底在数组头部
以下实现的就是数组栈
2、链式栈
双向链表栈底可以是尾也可以是头
单链表头插头删方便所以栈顶在链表尾部
单链表的头插头删方便在 头插加减新节点只需要修改头指针指向新的节点时间复杂度通常是O(1) 尾插需要遍历整个链表才能找到最后一个节点并将其之后的位置设置为新节点或者释放结点时间复杂度是O(n)
栈的创建 栈的打印
以下是单链表的打印
此为栈的打印 栈访问一遍就已经空了
内存泄漏
后端开发比如游戏上线了之后除了升级就不能退出所以如果存在内存泄漏就会导致游戏越来越慢因为可用内存资源越来越少
如果是在前端操作系统里出现了内存泄漏的情况在进程结束掉之后就会主动释放空间所以危害性不大
栈溢出
指的是给这个栈划分的内存区域爆了比如写了一个死循环的递归
Stack.h
#pragma once
#includestdio.h
#includeassert.h
#includestdlib.h
#includestdbool.htypedef int STDataType;typedef struct Stack {int* a;int top;//标识栈顶位置int capacity;//容量
}ST;void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
Stack.c
#define _CRT_SECURE_NO_WARNINGS
#includeStack.h
void STInit(ST* pst) {assert(pst);pst-a NULL;pst-capacity 0;pst-top 0;
}
void STDestroy(ST* pst) {assert(pst);free(pst-a);pst-a NULL;pst-capacity 0;pst-top 0;
}
void STPush(ST* pst, STDataType x) {assert(pst);if (pst-top pst-capacity) {int newcapacity pst-capacity 0 ? 4 : pst-capacity * 2;STDataType* tmp (STDataType*)realloc(pst-a, sizeof(STDataType) * newcapacity);if (tmp NULL) {perror(relloc);return;}pst-a tmp;pst-capacity newcapacity;}pst-a[pst-top] x;//在a[0]的时候放入第一个值pst-top;
}
void STPop(ST* pst) {assert(pst);assert(pst-top 0);//不为空pst-top--;
}STDataType STTop(ST* pst) {assert(pst);assert(pst-top 0);//不为空return pst-a[pst-top - 1];
}
bool STEmpty(ST* pst) {assert(pst);return pst-top 0;//等于0返回真不等于0返回假
}
int STSize(ST* pst) {assert(pst);return pst-top;
}
Test.c
#define _CRT_SECURE_NO_WARNINGS
#includeStack.hvoid Test() {ST s;STInit(s);STPush(s, 1);STPush(s, 2);STPush(s, 3);STPush(s, 4);while (!STEmpty(s)) {printf(%d , STTop(s));//打印栈顶位置的数据STPop(s);//弹栈}printf(\n);
}int main() {Test();return 0;
}
练习
有效的括号 数量和顺序都要匹配
#pragma once
#includestdio.h
#includeassert.h
#includestdlib.h
#includestdbool.htypedef char STDataType;typedef struct Stack {STDataType* a;int top;//标识栈顶位置int capacity;//容量
}ST;void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);void STInit(ST* pst) {assert(pst);pst-a NULL;pst-capacity 0;pst-top 0;
}
void STDestroy(ST* pst) {assert(pst);free(pst-a);pst-a NULL;pst-capacity 0;pst-top 0;
}
void STPush(ST* pst, STDataType x) {assert(pst);if (pst-top pst-capacity) {int newcapacity pst-capacity 0 ? 4 : pst-capacity * 2;STDataType* tmp (STDataType*)realloc(pst-a, sizeof(STDataType) * newcapacity);if (tmp NULL) {perror(relloc);return;}pst-a tmp;pst-capacity newcapacity;}pst-a[pst-top] x;//在a[0]的时候放入第一个值pst-top;
}
void STPop(ST* pst) {assert(pst);assert(pst-top 0);//不为空pst-top--;
}STDataType STTop(ST* pst) {assert(pst);assert(pst-top 0);//不为空return pst-a[pst-top - 1];
}
bool STEmpty(ST* pst) {assert(pst);return pst-top 0;//等于0返回真不等于0返回假
}
int STSize(ST* pst) {assert(pst);return pst-top;
}bool isValid(char* s) {ST st;STInit(st);while(*s){if(*s { || *s [ || *s (){//*s是左括号就入栈STPush(st, *s);}else{//右比左多会导致循环时栈为空还要弹栈if(STEmpty(st)){//防止内存泄漏STDestroy(st);return false;}//*s是右括号就与栈顶匹配char top STTop(st);STPop(st);//检查顺序匹配if((*s } top ! {)||(*s ] top ! [)||(*s ) top ! ()){STDestroy(st);return false;}}s;}//检查数量匹配//左多右少bool ret STEmpty(st);//等于0就返回真不等于0就返回假STDestroy(st);return ret;
}
文章转载自: http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.wjrq.cn.gov.cn.wjrq.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.drgmr.cn.gov.cn.drgmr.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.fjglf.cn.gov.cn.fjglf.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn