面向对象数据结构.pptVIP

  • 2
  • 0
  • 约1.47万字
  • 约 64页
  • 2021-08-30 发布于山西
  • 举报
?版权所有,转载或翻印必究 Page * C顺序栈的出栈操作 int Pop(SeqStack * S, StackElementType *x) { if(S-top==-1) /*栈为空*/ return(FALSE); else { *x= S-elem[S-top]; S-top--; /* 修改栈顶指针 */ return(TRUE); } } ?版权所有,转载或翻印必究 Page * C链栈定义 typedef struct node { StackElementType data; struct node *next; } LinkStackNode; typedef LinkStackNode *LinkStack; ?版权所有,转载或翻印必究 Page * C链栈的进栈操作 int Push(LinkStack top, StackElementType x) /* 将数据元素x压入栈top中 */ { LinkStackNode * temp; temp=(LinkStackNode * )malloc(sizeof(LinkStackNode)); if (temp==NULL) return(FALSE); /* 申请空间失败 */ temp-data=x; temp-next=top-next; top-next=temp; /* 修改当前栈顶指针 */ return(TRUE); } ?版权所有,转载或翻印必究 Page * C链栈的出栈操作 int Pop(LinkStack top, StackElementType *x) { /* 将栈top的栈顶元素弹出,放到x所指的存储空间中 */ LinkStackNode * temp; temp=top-next; if(temp==NULL) /*栈为空*/ return(FALSE); top-next=temp-next; *x=temp-data; free(temp); /* 释放存储空间 */ return(TRUE); } ?版权所有,转载或翻印必究 Page * 顺序栈:C版本括号匹配算法 void BracketMatch(char *str) { SeqStack S; int i; char ch; InitStack(S); for(i=0; str[i]!=\0; i++) { switch(str[i]) { case (: case [: case {: Push(S,str[i]); break; case ): case ]: case }: if (IsEmpty(S)) { printf(\n右括号多余!); return; } else { GetTop (S,ch); if (Match(ch,str[i])) Pop(S,ch); else { printf(\n括号不匹配!); return; } } /*else*/ }/*switch*/ }/*for*/ if (IsEmpty(S)) printf(\n括号匹配!); else printf(\n左括号多余 ); } ?版权所有,转载或翻印必究 Page * 链栈:C版本括号匹配算法 void BracketMatch(char *str) { LinkStack S; int i; char ch; InitStack(/**/S);

文档评论(0)

1亿VIP精品文档

相关文档