鱼C论坛

 找回密码
 立即注册
查看: 268|回复: 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 函数中,需要增加栈满的判断。

修改后代码:

  1. #include <stdio.h>
  2. #include <stdlib.h> // 添加stdlib.h以使用malloc和free

  3. #define MAXSIZE 10
  4. typedef struct {
  5.     int data[MAXSIZE];
  6.     int top1;
  7.     int top2;
  8. } DStack;

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

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

  17. bool Push(DStack *&s, int e, int which) {
  18.     if (StackFull(s)) {
  19.         return false;
  20.     }
  21.     if (which == 1) {
  22.         s->data[++s->top1] = e;
  23.     } else if (which == 2) {
  24.         s->data[--s->top2] = e;
  25.     }
  26.     return true;
  27. }

  28. bool Pop(DStack *&s, int &e, int which) {
  29.     if (which == 1 && s->top1 != -1) {
  30.         e = s->data[s->top1--];
  31.         return true;
  32.     } else if (which == 2 && s->top2 != MAXSIZE) {
  33.         e = s->data[s->top2++];
  34.         return true;
  35.     }
  36.     return false;
  37. }

  38. int main() {
  39.     DStack *s;
  40.     int e;
  41.     InitDStack(s);
  42.     Push(s, 1, 1);
  43.     Push(s, 2, 1);
  44.     Push(s, 3, 2);
  45.     if (Pop(s, e, 1)) {
  46.         printf("出栈:%d\n", e);
  47.     } else {
  48.         printf("栈为空或者出现错误\n");
  49.     }
  50.    
  51.     // 清理分配的内存
  52.     free(s);
  53.    
  54.     return 0;
  55. }
复制代码

最佳答案

查看完整内容

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

使用道具 举报

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

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

修改后代码:

  1. #include <stdio.h>
  2. #include <stdlib.h> // 添加stdlib.h以使用malloc和free

  3. #define MAXSIZE 10
  4. typedef struct {
  5.     int data[MAXSIZE];
  6.     int top1;
  7.     int top2;
  8. } DStack;

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

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

  17. bool Push(DStack *&s, int e, int which) {
  18.     if (StackFull(s)) {
  19.         return false;
  20.     }
  21.     if (which == 1) {
  22.         s->data[++s->top1] = e;
  23.     } else if (which == 2) {
  24.         s->data[--s->top2] = e;
  25.     }
  26.     return true;
  27. }

  28. bool Pop(DStack *&s, int &e, int which) {
  29.     if (which == 1 && s->top1 != -1) {
  30.         e = s->data[s->top1--];
  31.         return true;
  32.     } else if (which == 2 && s->top2 != MAXSIZE) {
  33.         e = s->data[s->top2++];
  34.         return true;
  35.     }
  36.     return false;
  37. }

  38. int main() {
  39.     DStack *s;
  40.     int e;
  41.     InitDStack(s);
  42.     Push(s, 1, 1);
  43.     Push(s, 2, 1);
  44.     Push(s, 3, 2);
  45.     if (Pop(s, e, 1)) {
  46.         printf("出栈:%d\n", e);
  47.     } else {
  48.         printf("栈为空或者出现错误\n");
  49.     }
  50.    
  51.     // 清理分配的内存
  52.     free(s);
  53.    
  54.     return 0;
  55. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-6 15:36:51 | 显示全部楼层
首先,你定义的是一个共享栈结构体 `DStack`,但是在 `InitDStack` 函数里没有为它分配内存空间,所以程序运行时会出错。可以修改代码如下:
  1. void InitDStack(DStack *&s) {
  2.     s = (DStack *)malloc(sizeof(DStack));
  3.     s->top1 = -1;
  4.     s->top2 = MAXSIZE;
  5. }
复制代码

此外,在 `Push` 函数中,你只是将元素赋值给了 `s->data`,但是没有改变 `top1` 和 `top2` 的值。你需要修改代码如下:
  1. bool Push(DStack *&s, int e, int which) {
  2.     if (StackFull(s)) {
  3.         return false;
  4.     }
  5.    
  6.     if (which == 1) {
  7.         s->top1++;
  8.         s->data[s->top1] = e;
  9.     } else if (which == 2) {
  10.         s->top2--;
  11.         s->data[s->top2] = e;
  12.     }
  13.    
  14.     return true;
  15. }
复制代码

然后,在 `Pop` 函数中,你只是将 `s->data` 的值赋给了 `e`,但是没有更新 `top1` 和 `top2` 的值。你需要修改代码如下:
  1. bool Pop(DStack *&s, int &e, int which) {
  2.     if (StackEmpty(s, which)) {
  3.         return false;
  4.     }
  5.    
  6.     if (which == 1) {
  7.         e = s->data[s->top1];
  8.         s->top1--;
  9.     } else if (which == 2) {
  10.         e = s->data[s->top2];
  11.         s->top2++;
  12.     }
  13.    
  14.     return true;
  15. }
复制代码

最后,在 `GetTop` 函数中,你只是将 `s->data` 的值赋给了 `e`,但是没有更新 `top1` 和 `top2` 的值。你需要修改代码如下:
  1. bool GetTop(DStack *s, int &e, int which) {
  2.     if (StackEmpty(s, which)) {
  3.         return false;
  4.     }
  5.    
  6.     if (which == 1) {
  7.         e = s->data[s->top1];
  8.     } else if (which == 2) {
  9.         e = s->data[s->top2];
  10.     }
  11.    
  12.     return true;
  13. }
复制代码

修改完以上代码后,再运行你的程序,应该可以正确输出结果了。

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 11:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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