c - 我的程序在 C 中实现堆栈有什么问题

标签 c data-structures stack-overflow

嗨,我的堆栈数据结构程序有问题。看来,当我定义数组的大小/数组的虚构大小只是为了通过循环调用它时,当我输入数据或推送时,我定义或由用户指定的大小将被耗尽或进行一些编辑。

例如。我输入 5 作为尺寸,然后选择推送,然后添加 2。它工作正常。但如果我选择再次推送数据,它现在将传递给大小变量。我不知道发生了什么......

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define p printf
#define s scanf

int top;
int ar[1];
int size;

main()
{
    void push();
    int opt, num;
    char cont[] = { 'y' };
    clrscr();

    p("Stacking Program");
    p("\n\nData Size: ");
    s("%d", &size);
    p("\n");

    while((cont[0] == 'y') || (cont[0] == 'Y'))
    {
        clrscr();
        p("Stacking Program");
        p("\n\nData Size: %d\n\n", size);
        p("MAIN MENU\n1. Pop\n2. Push\n3. Pick\n4. View\nChoose: ");
        s("%d", &opt);
        p("\n");

        switch(opt) {
            case 1:
                pop();
                break;
            case 2:
                if(top > size)
                {
                    p("You can't push more data");
                }
                else
                {
                    p("Enter data for Data[%d]: ", top);
                    s("%d", &num);
                    push(num);
                }
                break;
            case 3:
                pick();
                break;
            case 4:
                view();
                break;
            default:
                p("Your choice is not in the list.");
                break;
        }

        p("\n\nDo you want continue\(Y\/N\)?");
        s("%s", &cont[0]);
    }
}

pop()
{
    int a;
    if(top < 0)
    {
        p("Stack empty.");
        return 0;
    }
    else
    {
        a = ar[top];
        p("\(Data[%d] = %d\) removed.", top, a);
        top--;
    }
}
void push(int b)
{
    top++;
    ar[top] = b;
}
pick()
{
    if(top < 0)
    {
        p("Nothing to display.");
        return 0;
    }
    else
    {
        p("\(Data[%d] = %d\) is the last data.", top, ar[top]);
    }
}
view()
{
    int i;
    if(top < 0)
    {
        p("Nothing to display.");
        return 0;
    }
    else
    {
        for(i = 1; i < (top + 1); i++)
        {
            p("Data[%d] = %d\n", i, ar[i]);
        }
    }
}

最佳答案

您需要在运行时使用用户输入的大小来定义数组的大小。 而不是:

int top;
int ar[1];
int size;

...

int top = -1;
int *ar = NULL;
int size = 0;

然后从用户那里获取尺寸后:

   if ( size > 0 )
   {
      ar = malloc(size * sizeof(int));
      if ( ar == NULL )
      {
         printf("ERROR: malloc() failed\n");
         exit(2);
      }
   }
   else
   {
      printf("ERROR: size should be positive integer\n");
      exit(1);
   }


   ....
      p("\n\nDo you want continue(Y/N)?");
      s("%s", &cont[0]);
   }
   free(ar);


} // end of main

我认为 view() 中的 for 循环应该是:

 for(i = 0 ; i <= top ; i++)

还有

case 2:
    if ( top == ( size - 1 ))

关于c - 我的程序在 C 中实现堆栈有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12631072/

相关文章:

c++ - 指向给定内存地址的指针-C++

c++ - 如何在 C++ 中查找大数模 1000000007 的阶乘?

algorithm - 按开始时间对完成的请求进行排序

android - 每次我向菜单部分添加新的 xml 文件时,Eclipse 都会崩溃 - Android

c# - 堆栈溢出与快速排序实现

C 平方时答案错误

c - 基础C题(初级)

java - 按满足一定条件的值移除元素

data-structures - Fortran中类似动态哈希的数据结构

java - 为什么我收到堆栈溢出?