user-0 提问: 关于栈的问题。if语句里面哪里错了啊？

回复:
user: user-1
content: #include<stdio.h> #include<malloc.h> #include<string.h> #define STACK_INIT_SIZE 10 #define STACK_GROW_SIZE 5 #define ELEMTYPE char #define OK 1 #define ERROR 0 typedef struct { /*建立一个栈的首结点*/     ELEMTYPE * base;     ELEMTYPE * top;     int stacksize; } SpStack; int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/     s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));     if (!s->base) return ERROR;     s->top=s->base;     s->stacksize=STACK_INIT_SIZE;     return OK; } int StackEmpty(SpStack *s) { /*判断栈是否为空*/     if (s->top==s->base) return OK;     else                 return ERROR; } int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/     if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/         s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));         if (!s->base) return ERROR;         s->stacksize+=STACK_GROW_SIZE;         s->top=s->base+s->stacksize;     }     *s->top++=e;     return OK; } int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/     if (StackEmpty(s)) return ERROR;     *e=*(--s->top);     return OK; } int Comp(ELEMTYPE a,ELEMTYPE b) {     if ((a=='('&&b!=')')       ||(a=='['&&b!=']')       ||(a=='{'&&b!='}')) {         return ERROR;     } else return OK; } int Count(SpStack *s) {     ELEMTYPE e[STACK_INIT_SIZE*2];     ELEMTYPE e1;     int i;      InitStack(s);     fgets(e,STACK_INIT_SIZE*2,stdin);     if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;     printf("%s\n",e);     for (i=0;e[i]!='\0';i++) {         switch (e[i]) {         case '(':         case '[':         case '{':             Push(s,e[i]);             break;         case ')':         case ']':         case '}':             if (StackEmpty(s)) {                 printf("%*s↖右括号多余\n",i+1,"");                 return(ERROR);             } else Pop(s,&e1);             if (!Comp(e1,e[i])) {                 printf("%*s↖左右匹配出错\n",i+1,"");                 return(ERROR);             }         }     }     if (!StackEmpty(s)) {         printf("%*s↖左括号多余\n",i,"");         return(ERROR);     } else {         printf("匹配正确\n");         return(OK);     } } void main() {     SpStack s;     Count(&s);     free(s.base); }

user: user-2
content: 我去，回错帖子了

user: user-2
content: #include<stdio.h> #include<malloc.h> #include<string.h> typedef char elemtype; typedef struct stacknode{     elemtype data;     struct stacknode *next; }StackNode; //建栈 StackNode *InitStack(StackNode *top){                  //你这是在做什么？？？？又申请空间，又复制NULL，你到底想干啥？     top=(StackNode *)malloc(sizeof(StackNode));     top=NULL;     return top; } int Judge(StackNode *top){     if(top==NULL) 	{ 		printf("建栈成功.\n"); 		return 1; 	}     else     {         printf("建栈失败！\n");         return 0;     } } //进栈 int Push(StackNode *top){     StackNode *p,*q;     char *ch = (char*)malloc(sizeof(char)*20);        ///只改了一下先把字符串接收完了， 	gets(ch);                                                       ////然后逐一分析，具体你的方法该怎么改还没时间帮你看     while('#' != *ch) 	{ 		if(*ch=='{'||*ch=='('||*ch=='['||*ch=='<')         {             p=(StackNode *)malloc(sizeof(StackNode));//如果是左边的，进栈。             p->next=NULL;             p->data=*ch;             p->next=top;             top=p;         }         if(*ch=='}'||*ch==')'||*ch==']'||*ch=='>')         {             if(top==NULL) 			{ 				printf("栈空.\n");//如果是右边的且top=NULL栈空，说明不平衡。还有符号没有平衡。 				return 0; 			}             else 			{ 				switch(*ch) 				{ 				case '{':if(top->data!='}')//如果是右边的且top！=NULL出栈 							 return 0; 					else 						break; 				case '(':if(top->data!=')') 							 return 0; 					else 						break; 				case '[':if(top->data!=']') 							 return 0; 					else 						break; 				case '<':if(top->data!='>') 							 return 0; 					else 						break; 				} 				q=top; 				top=top->next; 				free(q); 			}         } 		++ch;     }     if(top==NULL)         return 1;     else         return 0; };  int main(){ 	StackNode *top = NULL; 	int k; 	//top=InitStack(top); 	Judge(top); 	printf("请输入带{} () [] <>的字符的字符串以“#”号键结束:"); 	k=Push(top); 	if(k==1) 		printf("符号平衡.\n"); 	else 		printf("符号不平衡.\n"); 	return 0; }

user: user-3
content: 引用 1 楼 user-4 的回复:                if(s==top->data)                 {                     q=(StackNode *)malloc(sizeof(StackNode));                     q->next=NULL;                     q=top->data;                     top=top->next;                     free(q);                 }   这里释放的是刚分配的内存，原来栈里面的没有释放 楼上正解，应该是q = top; top = top->next;free(p);

user: user-4
content: if(s==top->data)                 {                     q=(StackNode *)malloc(sizeof(StackNode));                     q->next=NULL;                     q=top->data;                     top=top->next;                     free(q);                 }   这里释放的是刚分配的内存，原来栈里面的没有释放