平衡的支架检查器生成不正确的输出

发布于 2025-01-30 08:24:33 字数 2386 浏览 5 评论 0 原文

我创建了一个使用链接列表来检查表达式是否平衡的函数。但是函数'括号平衡'始终给出“不平衡”,无论输入是什么,输出。

注意:平衡的表达没有。打开括号的'(“等于关闭括号的号码”)'

代码:

#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上面的示例,尽管表达式平衡,我的代码会生成不正确的输出。请更正我的代码。

I have created a function which uses Linked List to check whether an expression is balanced or not. But the function 'Bracket Balancing' always gives 'unbalanced' as the output no matter what the input is.

NOTE: A balanced expression has no. of opening brackets '(' equal to no. of closing brackets ')'

CODE:

#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;
}

Example:

Input:

Enter the length of the expression for Bracket Balancing 
4
Enter the expression for Bracket Balancing
1+()

Output:

This expression is unbalanced

In the above example, despite the expression being balanced my code generates incorrect output. Please correct my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2025-02-06 08:24:33

这就是您初始化列表的方式:

struct LL *top = malloc(sizeof(struct LL));
top->next = NULL;

这是 isempty()

int isEmpty(struct LL *top)
{
    if (top == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

但是 top 以value!= null,所以 Isemtpy()不会返回1,尽管我们的列表在开始时应该为空。

您的 push()的实现应在通过NULL时工作正常,因此您可以初始化 struct ll *top = null; ,而不是在右下创建第一个元素。

代码中还有其他错误,例如:

  • in pop()您做

      struct ll *n = malloc(sizeof(struct ll));
      n = top;
     

这样做, malloc()的结果

  • isfull()的 下一行中直接覆盖()。您调用 malloc()时,内存泄漏,切勿使用或 free()缓冲区返回。无论如何,该功能都没有意义,只需检查 malloc() s的结果,您真正想要使用返回缓冲区。

**编辑**

我以前从未见过的东西,您也永远不会使用 push() pop()的返回值,因此这些函数确定的新顶部迷路了。替换 push(top,...); by top = push(top,...); and pop(top); by by top = pop(top);

This is how you initialize your list:

struct LL *top = malloc(sizeof(struct LL));
top->next = NULL;

And this is isEmpty():

int isEmpty(struct LL *top)
{
    if (top == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

But: top starts with a value != NULL, so isEmtpy() 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 initialize struct LL *top = NULL; instead of creating the first element rightaway.

there other bugs in your code, e.g.:

  • in pop() you do

      struct LL *n = malloc(sizeof(struct LL));
      n = top;
    

thus, the result of malloc() is directly overwritten() in the next line

  • in isFull() you produce a memory leak as you call malloc() and never use or free() the buffer returned. That function doesn't make sense anyway, just check the result of malloc()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() and pop() so the new top determined by these function is lost. Replace push(top, ...); by top = push(top,...); and pop(top); by top = pop(top);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文