2301_81829019 2024-04-11 22:53 采纳率: 66.7%
浏览 5
已结题

c++栈,按一定顺序入栈,给定出栈顺序,判断能否以此出栈

一段循环出错了

#include <stdio.h>
#define Maxsize 100
typedef int ElemType;

typedef struct {
    ElemType data[Maxsize];
    int top;
} SqStack;

void InitStack(SqStack &st) {
    st.top = -1;
}

void DestoryStack(SqStack st)
{}

int Push(SqStack &st, ElemType x) {
    if (st.top == Maxsize - 1)
        return 0;
    else {
        st.top++;
        st.data[st.top] = x;
        return 1;
    }
}

int Pop(SqStack &st, ElemType &x) {
    if (st.top == -1)
        return 0;
    else {
        x = st.data[st.top];
        st.top--;
        return 1;
    }
}

int GetTop(SqStack st, ElemType &x) {
    if (st.top == -1)
        return 0;
    else {
        x = st.data[st.top];
        return 1;
    }
}

int StackEmpty(SqStack st) {
    if (st.top == -1)
        return 1;
    else
        return 0;
}

int main() {
    SqStack st;
    InitStack(st);
    ElemType n, i;
    ElemType k = 0;
    ElemType k1 = 0;
    ElemType tem[20];
    ElemType tem1[20];
    ElemType e;
    ElemType flag = 0;
    printf("栈的长度:");
    scanf("%d", &n);
    printf("\n");
    printf("入栈的次序:");
    for (i = 0; i < n; i++)
        scanf("%d", &tem[i]);
    printf("\n");
    printf("出栈的次序:");
    for (i = 0; i < n; i++)
        scanf("%d", &tem1[i]);
    printf("\n");
    printf("判断结果:");
    printf("\n");
    //

    for (k; k < n; k++) {
//        e = tem[k];
        Push(st, tem[k]);
        printf("%d进栈", tem[k]);
        printf("\n");//输入序列一个个入栈
        while (st.data[st.top] == tem1[k1]) { //判断栈顶元素与输出序列的第k1个元素是否相等(相等则出栈)
            Pop(st, tem[k]);
            k1++;
            printf("%d出栈", tem[k]);
            printf("\n");
        }

    }
//
    if (StackEmpty(st)) {
        printf("结束");
    } else
        printf("不行");
    return 0;
}

运行结果如下

img

  • 写回答

3条回答 默认 最新

  • 码农阿豪 Java领域优质创作者 2024-04-11 22:54
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题:该段代码实现了一个栈,并进行了入栈出栈操作。但是在实现过程中,有一段代码被注释掉了(//之间的部分),导致程序运行结果与预期不符。请说明原因,并给出正确的代码实现。 回答: 原因:循环中注释掉的部分是用来将输入序列依次入栈的,但是实际上该部分代码没有被执行,导致栈内没有将所有的元素入栈,而直接进行了出栈操作,导致程序运行结果出错。 正确的代码实现如下:
    #include <stdio.h>
    #define Maxsize 100
    typedef int ElemType;
    typedef struct {
        ElemType data[Maxsize];
        int top;
    } SqStack;
    void InitStack(SqStack &st) {
        st.top = -1;
    }
    void DestoryStack(SqStack st){}
    int Push(SqStack &st, ElemType x) {
        if (st.top == Maxsize - 1)
            return 0;
        else {
            st.top++;
            st.data[st.top] = x;
            return 1;
        }
    }
    int Pop(SqStack &st, ElemType &x) {
        if (st.top == -1)
            return 0;
        else {
            x = st.data[st.top];
            st.top--;
            return 1;
        }
    }
    int GetTop(SqStack st, ElemType &x) {
        if (st.top == -1)
            return 0;
        else {
            x = st.data[st.top];
            return 1;
        }
    }
    int StackEmpty(SqStack st) {
        if (st.top == -1)
            return 1;
        else
            return 0;
    }
    int main() {
        SqStack st;
        InitStack(st);
        ElemType n, i;
        ElemType tem[20];
        ElemType tem1[20];
        ElemType e;
        ElemType flag = 0;
        printf("栈的长度:");
        scanf("%d", &n);
        printf("\n");
        printf("入栈的次序:");
        for (i = 0; i < n; i++)
            scanf("%d", &tem[i]);
        printf("\n");
        printf("出栈的次序:");
        for (i = 0; i < n; i++)
            scanf("%d", &tem1[i]);
        printf("\n");
        printf("判断结果:");
        printf("\n");
        for (i = 0; i < n; i++) {
            e = tem[i];
            Push(st, tem[i]);
            printf("%d进栈", tem[i]);
            printf("\n");
            while (st.data[st.top] == tem1[flag]) {
                Pop(st, tem[i]);
                printf("%d出栈", tem[i]);
                printf("\n");
                flag++;
                if (StackEmpty(st))
                    break;
            }
        }
        if (StackEmpty(st)) {
            printf("结束");
        } else
            printf("不行");
        return 0;
    }
    

    经过修改后,该程序运行结果如下: img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月12日
  • 已采纳回答 4月12日
  • 创建了问题 4月11日

悬赏问题

  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图
  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
  • ¥15 python螺旋图像
  • ¥15 算能的sail库的运用
  • ¥15 'Content-Type': 'application/x-www-form-urlencoded' 请教 这种post请求参数,该如何填写??重点是下面那个冒号啊
  • ¥15 找代写python里的jango设计在线书店
  • ¥15 请教如何关于Msg文件解析
  • ¥200 sqlite3数据库设置用户名和密码