数据结构--利栈完成括号匹配检验和行编辑算法附源程序(转).docVIP

  • 12
  • 0
  • 约1.49万字
  • 约 7页
  • 2016-10-12 发布于贵州
  • 举报

数据结构--利栈完成括号匹配检验和行编辑算法附源程序(转).doc

数据结构--利栈完成括号匹配检验和行编辑算法附源程序(转)

这个文件是数据存储设计:tochar.h #ifndef TOCHAR_H #define TOCHAR_H #includestdio.h #includestdlib.h #define TRUE? ?? ???1 #define FALSE? ?? ? 0 #define OK? ?? ?? ? 1 #define ERROR? ?? ? 0 #define INFEASIBLE??-1 #define OVERFLOW? ? -2 typedef long ElemType; typedef int Status; typedef struct node { ElemType elem; struct node *next; struct node *prior; }Node,*Postion; typedef struct { Node *base,*top; int length; }Stack; Stack *initstack();? ?? ?? ?? ?? ?? ???//初始化栈 Status creatstack(Stack *s);? ?? ?? ???//随机创建一个栈 void??printstack(Stack const *s);? ?? ?//打印栈 Node *partition(Node *,Node *); void Qsort(Node *,Node *); Status destroy(Stack *);? ?? ?? ?? ?? ?//销毁整个栈 Status deltop(Stack *s);? ?? ?? ?? ?? ?//出栈 Status pushelem(Stack *,ElemType );? ? //进栈 Stack *initstack() { Stack *s; s=(Stack *)malloc(sizeof(Stack)); if(!s) return ERROR; s-base=s-top=NULL;? ? //栈空 s-length=0; return s; } Status creatstack(Stack *s) { Node *n; int i; ElemType e; //i=rand()%10+1; i=5; s-length=i;? ? //栈长 ? ? n=(Node *)malloc(sizeof(Node)); ? ? if(!n) return ERROR; 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-prior=s-top; ??s-top-next=n; ??s-top=n; ??i--; }while(i-1); s-top-next=NULL; printstack(s); ? ? return (OK); } void printstack(Stack const *s) { ? ? Node *N; N=s-base; for(;N;N=N-next) { ??printf(%3c,N-elem); } printf(\n\n); } Node *partition(Node *low,Node *high) { ElemType 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; return low; } void Qsort(Node *low,Node *high) { Node *temp; temp=partition(low,high); if(low!=temp) { ?? ??Qsort(low,temp-prior); } if(high!=temp) { ?? ??Qsort(temp-next,high); ? ? } } Status destroy(Stack *s) { Node *p; p=s-base;?? while(s-base-next) { ??s-base=s-base-next;? ?//N=N-next和free(P)不能倒换位置,当释放p时, ??free(p);? ???//如果

文档评论(0)

1亿VIP精品文档

相关文档