鱼C论坛

 找回密码
 立即注册
查看: 457|回复: 2

[已解决]共享栈问题

[复制链接]
发表于 2023-12-6 15:34:22 | 显示全部楼层 |阅读模式
10鱼币
#include<stdio.h>
#define MAXSIZE 10
typedef struct {
int data[MAXSIZE];
int top1;
int top2;
} DStack;

void InitDStack(DStack *&s) {
s->top1 = -1;
s->top2=MAXSIZE;
}


bool StackEmpty(DStack *s, int which) {
if(which==1)
{
    return(s->top1==-1);
}
if(which==2)
{
   return(s->top2==MAXSIZE);
}
}
bool StackFull(DStack *s)
{
return s->top1+1==s->top2;
}

bool Push(DStack *&s, int e, int which) {
// which指示新元素e进哪个栈
if(which==1)
{
   s->data[++s->top1]=e;
}
if(which==2)
{
   s->data[--s->top2]=e;
}
return true;
}

bool Pop(DStack *&s, int &e, int which) {
// which指示从哪个栈弹出栈顶元素赋给e
if(which==1)
{
   e=s->data[s->top1--];
}
if(which==2)
{
   e=s->data[s->top2++];
}
return true;
}

bool GetTop(DStack *s, int &e, int which) {
// which指示取哪个栈的栈顶元素赋给e
if(which==1)
{
   e=s->data[s->top1];
}
if(which==2)
{
   e=s->data[s->top2];
}
return true;
}

int main()
{
        DStack *s;
        int e;
        InitDStack(s);
        Push(s,1,1);
        Push(s,2,1);
        Push(s,3,2);
        Pop(s,e,1);
        printf("出栈:%d\n",e);
       
        return 0;
}

为什么输出没有值?怎么修改呢?
最佳答案
2023-12-6 15:34:23
代码中存在几个问题导致没有按预期输出值:

  • 在 main 函数中,你声明了 DStack *s 指针,但没有为它分配内存。你需要使用 malloc 或者在栈上分配内存。
  • 在 Push 和 Pop 函数中,你应该检查栈是否已满或为空,以防止溢出或下溢。
  • 在 Push 函数中,需要增加栈满的判断。

修改后代码:
#include <stdio.h>
#include <stdlib.h> // 添加stdlib.h以使用malloc和free

#define MAXSIZE 10
typedef struct {
    int data[MAXSIZE];
    int top1;
    int top2;
} DStack;

void InitDStack(DStack *&s) {
    s = (DStack *)malloc(sizeof(DStack)); // 为DStack分配内存
    s->top1 = -1;
    s->top2 = MAXSIZE;
}

bool StackFull(DStack *s) {
    return s->top1 + 1 == s->top2;
}

bool Push(DStack *&s, int e, int which) {
    if (StackFull(s)) {
        return false;
    }
    if (which == 1) {
        s->data[++s->top1] = e;
    } else if (which == 2) {
        s->data[--s->top2] = e;
    }
    return true;
}

bool Pop(DStack *&s, int &e, int which) {
    if (which == 1 && s->top1 != -1) {
        e = s->data[s->top1--];
        return true;
    } else if (which == 2 && s->top2 != MAXSIZE) {
        e = s->data[s->top2++];
        return true;
    }
    return false;
}

int main() {
    DStack *s;
    int e;
    InitDStack(s);
    Push(s, 1, 1);
    Push(s, 2, 1);
    Push(s, 3, 2);
    if (Pop(s, e, 1)) {
        printf("出栈:%d\n", e);
    } else {
        printf("栈为空或者出现错误\n");
    }
    
    // 清理分配的内存
    free(s);
    
    return 0;
}

最佳答案

查看完整内容

代码中存在几个问题导致没有按预期输出值: [*]在 main 函数中,你声明了 DStack *s 指针,但没有为它分配内存。你需要使用 malloc 或者在栈上分配内存。 [*]在 Push 和 Pop 函数中,你应该检查栈是否已满或为空,以防止溢出或下溢。 [*]在 Push 函数中,需要增加栈满的判断。 修改后代码:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-6 15:34:23 | 显示全部楼层    本楼为最佳答案   
代码中存在几个问题导致没有按预期输出值:

  • 在 main 函数中,你声明了 DStack *s 指针,但没有为它分配内存。你需要使用 malloc 或者在栈上分配内存。
  • 在 Push 和 Pop 函数中,你应该检查栈是否已满或为空,以防止溢出或下溢。
  • 在 Push 函数中,需要增加栈满的判断。

修改后代码:
#include <stdio.h>
#include <stdlib.h> // 添加stdlib.h以使用malloc和free

#define MAXSIZE 10
typedef struct {
    int data[MAXSIZE];
    int top1;
    int top2;
} DStack;

void InitDStack(DStack *&s) {
    s = (DStack *)malloc(sizeof(DStack)); // 为DStack分配内存
    s->top1 = -1;
    s->top2 = MAXSIZE;
}

bool StackFull(DStack *s) {
    return s->top1 + 1 == s->top2;
}

bool Push(DStack *&s, int e, int which) {
    if (StackFull(s)) {
        return false;
    }
    if (which == 1) {
        s->data[++s->top1] = e;
    } else if (which == 2) {
        s->data[--s->top2] = e;
    }
    return true;
}

bool Pop(DStack *&s, int &e, int which) {
    if (which == 1 && s->top1 != -1) {
        e = s->data[s->top1--];
        return true;
    } else if (which == 2 && s->top2 != MAXSIZE) {
        e = s->data[s->top2++];
        return true;
    }
    return false;
}

int main() {
    DStack *s;
    int e;
    InitDStack(s);
    Push(s, 1, 1);
    Push(s, 2, 1);
    Push(s, 3, 2);
    if (Pop(s, e, 1)) {
        printf("出栈:%d\n", e);
    } else {
        printf("栈为空或者出现错误\n");
    }
    
    // 清理分配的内存
    free(s);
    
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-6 15:36:51 | 显示全部楼层
首先,你定义的是一个共享栈结构体 `DStack`,但是在 `InitDStack` 函数里没有为它分配内存空间,所以程序运行时会出错。可以修改代码如下:
void InitDStack(DStack *&s) {    s = (DStack *)malloc(sizeof(DStack));    s->top1 = -1;    s->top2 = MAXSIZE;}
此外,在 `Push` 函数中,你只是将元素赋值给了 `s->data`,但是没有改变 `top1` 和 `top2` 的值。你需要修改代码如下:
bool Push(DStack *&s, int e, int which) {    if (StackFull(s)) {        return false;    }        if (which == 1) {        s->top1++;        s->data[s->top1] = e;    } else if (which == 2) {        s->top2--;        s->data[s->top2] = e;    }        return true;}
然后,在 `Pop` 函数中,你只是将 `s->data` 的值赋给了 `e`,但是没有更新 `top1` 和 `top2` 的值。你需要修改代码如下:
bool Pop(DStack *&s, int &e, int which) {    if (StackEmpty(s, which)) {        return false;    }        if (which == 1) {        e = s->data[s->top1];        s->top1--;    } else if (which == 2) {        e = s->data[s->top2];        s->top2++;    }        return true;}
最后,在 `GetTop` 函数中,你只是将 `s->data` 的值赋给了 `e`,但是没有更新 `top1` 和 `top2` 的值。你需要修改代码如下:
bool GetTop(DStack *s, int &e, int which) {    if (StackEmpty(s, which)) {        return false;    }        if (which == 1) {        e = s->data[s->top1];    } else if (which == 2) {        e = s->data[s->top2];    }        return true;}
修改完以上代码后,再运行你的程序,应该可以正确输出结果了。

另外,建议在栈满和栈空的判断函数 `StackFull` 和 `StackEmpty` 中加上对栈是否存在的判断,以避免潜在的空指针异常。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-20 15:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表