欢迎来到天天文库
浏览记录
ID:59355535
大小:23.00 KB
页数:7页
时间:2020-09-04
《数据结构--利用栈完成括号匹配检验和行编辑算法附源程序(转).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、这个文件是数据存储设计:tochar.h#ifndefTOCHAR_H#defineTOCHAR_H#include#include#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE -1#defineOVERFLOW -2typedeflongElemType;typedefintStatus;typedefstructnode{ElemTypee
2、lem;structnode*next;structnode*prior;}Node,*Postion;typedefstruct{Node*base,*top;intlength;}Stack;Stack*initstack(); //初始化栈Statuscreatstack(Stack*s); //随机创建一个栈void printstack(Stackconst*s); //打印栈Node*partition(Node*,Node*);voidQsort(
3、Node*,Node*);Statusdestroy(Stack*); //销毁整个栈Statusdeltop(Stack*s); //出栈Statuspushelem(Stack*,ElemType); //进栈Stack*initstack(){Stack*s;s=(Stack*)malloc(sizeof(Stack));if(!s)returnERROR;s->base=s->top=NULL; //栈空s->length=0;returns;}Statuscr
4、eatstack(Stack*s){Node*n;inti;ElemTypee;//i=rand()%10+1;i=5;s->length=i; //栈长 n=(Node*)malloc(sizeof(Node)); if(!n)returnERROR;s->base=s->top=n;e=rand()%100;n->elem=e;n->prior=NULL;do{ e=rand()%100; n=(Node*)malloc(sizeof(Node)); n->elem=e; n
5、->prior=s->top; s->top->next=n; s->top=n; i--;}while(i-1);s->top->next=NULL;printstack(s); return(OK);}voidprintstack(Stackconst*s){ Node*N;N=s->base;for(;N;N=N->next){ printf("%3c",N->elem);}printf("");}Node*partition(Node*low,Node*high){ElemType
6、key;key=low->elem;while(low!=high){ while((low!=high)&&((high->elem)>=key))high=high->prior; low->elem=high->elem; while((low!=high)&&((low->elem)<=key)) low=low->next; high->elem=low->elem;}low->elem=key;returnlow;}voidQsort(Node*low,Node*high){Node
7、*temp;temp=partition(low,high);if(low!=temp){ Qsort(low,temp->prior);}if(high!=temp){ Qsort(temp->next,high); }}Statusdestroy(Stack*s){Node*p;p=s->base; while(s->base->next){ s->base=s->base->next; //N=N->next和free(P)不能倒换位置,当释放p时, free(p); //如
8、果不将N移向下一个位置,将导致N指向的内存释放,N->next不再有效 p=s->base;}s->base=s->top=NULL; s->length=0;returnOK;}Statusdeltop(Stack*s){Node*p;if(s->top==NULL) printf("Underflow"); //已经是空栈else{ p=s->top; s->top=p->p
此文档下载收益归作者所有