平衡的支架检查器生成不正确的输出
我创建了一个使用链接列表来检查表达式是否平衡的函数。但是函数'括号平衡'始终给出“不平衡”,无论输入是什么,输出。
注意:平衡的表达没有。打开括号的'(“等于关闭括号的号码”)'
代码:
#include <stdio.h>
#include <stdlib.h>
struct LL{
char data;
struct LL *next;
};
int isEmpty(struct LL *top){
if (top == NULL){
return 1;
}
else{
return 0;
}
}
int isFull(struct LL *top){
struct LL *n = malloc(sizeof(struct LL *));
if (n == NULL){
return 1;
}
else{
return 0;
}
}
struct LL *push(struct LL *top, char x){
if (isFull(top)){
printf("Stack Overflow\n");
}
else{
struct LL *n = malloc(sizeof(struct LL));
n->data = x;
n->next = top;
top = n;
}
return top;
}
struct LL *pop(struct LL *top){
if (isEmpty(top)){
printf("Stack Underflow\n");
}
else{
struct LL *n = malloc(sizeof(struct LL));
n = top;
top = top->next;
free(n);
}
return top;
}
int BracketBalancing(char *exp){
struct LL *top = malloc(sizeof(struct LL));
top->next = NULL;
for (int i = 0; exp[i] != '\0'; i++){
if (exp[i] == '('){
push(top, exp[i]);
}
else if (exp[i] == ')'){
if (isEmpty(top)){
return 0;
}
pop(top);
}
}
if (isEmpty(top)){
return 1;
}
else{
return 0;
}
}
int main(int argc, char const *argv[]){
int n;
char *expression = (char *)malloc(sizeof(char));
printf("Enter the length of the expression for Bracket Balancing\n");
scanf("%d", &n);
printf("Enter the expression for Bracket Balancing\n");
for (int i = 0; i < n; i++){
scanf("%c ", &expression[i]);
}
getchar();
if (BracketBalancing(expression)){
printf("The expression is balanced\n");
}
else if (!BracketBalancing(expression)){
printf("This expression is unbalanced\n");
}
return 0;
}
示例:
输入:
Enter the length of the expression for Bracket Balancing
4
Enter the expression for Bracket Balancing
1+()
输出:
This expression is unbalanced
in上面的示例,尽管表达式平衡,我的代码会生成不正确的输出。请更正我的代码。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是您初始化列表的方式:
这是
isempty()
:但是:
top
以value!= null,所以Isemtpy()
不会返回1,尽管我们的列表在开始时应该为空。您的
push()
的实现应在通过NULL时工作正常,因此您可以初始化struct ll *top = null;
,而不是在右下创建第一个元素。代码中还有其他错误,例如:
in
pop()
您做这样做,
malloc()
的结果isfull()
的 下一行中直接覆盖()。您调用malloc()
时,内存泄漏,切勿使用或free()
缓冲区返回。无论如何,该功能都没有意义,只需检查malloc()
s的结果,您真正想要使用返回缓冲区。**编辑**
我以前从未见过的东西,您也永远不会使用
push()
和pop()
的返回值,因此这些函数确定的新顶部迷路了。替换push(top,...);
bytop = push(top,...);
andpop(top);
by bytop = pop(top);
This is how you initialize your list:
And this is
isEmpty()
:But:
top
starts with a value != NULL, soisEmtpy()
will not return 1, although our list should be empty in the beginning.Your implementation of
push()
should work fine when you pass NULL, so you can just initializestruct LL *top = NULL;
instead of creating the first element rightaway.there other bugs in your code, e.g.:
in
pop()
you dothus, the result of
malloc()
is directly overwritten() in the next lineisFull()
you produce a memory leak as you callmalloc()
and never use orfree()
the buffer returned. That function doesn't make sense anyway, just check the result ofmalloc()
s where your really want to use the buffer returned.** Edit **
What I haven't seen before, you also never use the return value of
push()
andpop()
so the new top determined by these function is lost. Replacepush(top, ...);
bytop = push(top,...);
andpop(top);
bytop = pop(top);