简体   繁体   English

尝试通过 C 中的结构实现堆栈,但得到以下代码的运行时错误。 谁能解释并指出出了什么问题?

[英]Tried implementing stack through structure in C but getting Runtime Error for the below code. Can anyone explain and point out as to what went wrong?

Here's an inline link to Github stack_implementation_using_structure这是Github stack_implementation_using_structure 的内联链接

#include <stdio.h>
#define MAXSIZE 5

struct stack
{
    int stk[MAXSIZE];
    int top;
};
typedef struct stack STACK;
STACK s;

void push(int);
void pop(void);
void display(void);

void main ()
{
    s.top = -1;
    push(1);
    push(2);
    push(3);
    push(4);
    push(5);
    display();
    push(6);
    pop();
    pop();
    pop();
    pop();
    pop();
    pop();
}
/*  Function to add an element to the stack */
void push (int num)
{
    if (s.top == (MAXSIZE - 1))
    {
        printf ("Stack is Full\n");
    }
    else
    {
        s.top++;
        s.stk[s.top] = num;
    }
}
/*  Function to delete an element from the stack */
void pop ()
{
    if (s.top == - 1)
    {
        printf ("Stack is Empty\n");
    }
    else
    {
        printf ("poped element is = %d\n", s.stk[s.top]);
        s.top--;
    }
}
/*  Function to display the status of the stack */
void display ()
{
    int i;
    if (s.top == -1)
    {
        printf ("Stack is empty\n");
    }
    else
    {
        printf ("The status of the stack is \n");
        for (i = s.top; i >= 0; i--)
        {
            printf ("%d ", s.stk[i]);
        }
    }
    printf ("\n");
}

The return type of the main function should be int , not void , and for a main with no parameters, the parameter list should be void : main function 的返回类型应该是int ,而不是void ,对于没有参数的main ,参数列表应该是void

int main (void)
{
    s.top = -1;
    /* ... */
    return 0; // can be omitted - see description below.
}

Although the C standard allows main 's execution to reach the end of the function without executing a return statement, I like to add a return 0;虽然 C 标准允许main的执行到达 function 的末尾而不执行return语句,但我喜欢添加一个return 0; for portability with earlier versions of the C standard that did not have this feature.为了便于使用没有此功能的早期版本的 C 标准。

It worked for me but be careful you're trying to put 6 elements in the stack with an array of 5 elements (with the MAXSIZE at 5).它对我有用,但要小心你试图将 6 个元素放入堆栈中,其中包含 5 个元素的数组(MAXSIZE 为 5)。 The last one will not be considered and it can create bigger problems.最后一个不会被考虑,它会产生更大的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行这个有点奇怪的 c 代码后出现意外的 output。 谁能解释这是怎么发生的? - Unexpected output after running this little bit strange c code. Can anyone explain how this happened? (cs50)没有得到给定代码的 output。 任何人都可以检查一下吗? - (cs50)not getting output for the given code. can anyone please check this out? 任何人都可以解释这段代码会做什么? - Can anyone explain what this code will do? 谁能解释代码putchar(&#39;/&#39;// * / 1)的含义 - Can anyone explain what the code putchar('/' //*/ 1) mean 有人可以帮我解释一下我的 c 代码有什么问题吗? - Can someone help me to explain what is wrong in my c code? 我的程序在C中实现堆栈有什么问题 - What is wrong with my program implementing a stack in C 任何人都可以向我解释为什么这个错误“'('令牌之前的预期标识符”会在我的代码中弹出? - Anyone can explain me why this error "expected identifier before '(' token" is popping out in my code? 如果执行下面的代码,输出将为8;否则,输出为8。 但是,我认为应该是10。有人可以解释这里发生的情况吗? - If you execute the code below, the output will be 8; however, I feel it should be 10. Can anyone explain what goes on here? 请解释这个 C 代码有什么问题? - Please explain What's wrong with this C code? 谁能解释下面的代码片段? - Could anyone explain the below code snippet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM