c - C 上的堆栈(简单程序)

标签 c stack

我是 C 语言的初学者,一直在尝试编写一个 Stack 程序,它可以拉/推并显示其中的元素。我遇到了一些问题,我无法确定它的根源。 到目前为止,这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define STACK_SIZE 100

char ch;
int contents[STACK_SIZE], top = 0;

void display();

int stack_overflow(void)
{
        printf("Expression is too complex\n");
        exit(EXIT_FAILURE);
}

int stack_underflow(void)
{
        printf("Not enough operands in expression\n");
        exit(EXIT_FAILURE);
}

void make_empty(void)
{
    top = 0;                //makes the element at the top of the stack = 0
}

bool is_empty(void)
{
    return top == 0;        //returns 1 if the top is equal to 0
}

bool is_full(void)
{
    return top == STACK_SIZE;   //returns 1 if the top is the maximun stack size (already full)
}

void push(char i)
{
    if (is_full())
        stack_overflow();
    else
        contents[top++] = i;
}

char pop(void)
{
    if (is_empty())
        stack_underflow();
    else
        return contents[--top];
}


int main(void)
{
    int choice;
    int option = 1;
    int num;



    printf ("STACK OPERATION\n");
    while (option)
    {
        printf ("------------------------------------------\n");
        printf (" 1 --> PUSH \n");
        printf (" 2 --> POP \n");
        printf (" 3 --> DISPLAY \n");
        printf (" 4 --> EXIT \n");
        printf ("------------------------------------------\n");

        printf ("Enter your choice\n");
        scanf ("%d", &choice);

        switch (choice)
        {
            case 1: printf("Enter number you want to push: ");
                    scanf("%d",&num);
                    push(num);
                    break;

            case 2: pop();
                    break;

            case 3: display();
                    break;

            case 4: return;
        }

        fflush (stdin);
        printf ("Do you want to continue(Type 0 or 1)?\n");
        scanf ("%d", &option);
    }
}



void display()
{
    int i;

    printf("\n\n");

    for(i=0;i< top ;i++)
        printf("%d\n",contents[i]);
}


STACK OPERATION
------------------------------------------
 1 --> PUSH
 2 --> POP
 3 --> DISPLAY
 4 --> EXIT
------------------------------------------
Enter your choice
1
Enter number you want to push: 100
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
 1 --> PUSH
 2 --> POP
 3 --> DISPLAY
 4 --> EXIT
------------------------------------------
Enter your choice
1
Enter number you want to push: 500
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
 1 --> PUSH
 2 --> POP
 3 --> DISPLAY
 4 --> EXIT
------------------------------------------
Enter your choice
3


100
-12
Do you want to continue(Type 0 or 1)?
0

进程返回 0 (0x0) 执行时间:11.474 秒 按任意键继续。

似乎它没有正确存储元素,或者可能打印错误。也许使用 top 变量来遍历循环是错误的?任何帮助将不胜感激。

最佳答案

你的推送函数只得到一个char:

void push(char i)

char 限制在 0 到 255(unsigned char)或 -127 到 128(signed char = char - 这是默认值)。

因此,您输入的“500”将减少(mod 256)为 -12 - 这就是您打印的内容。

您需要将参数设为 int,它应该可以工作。

关于c - C 上的堆栈(简单程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39339136/

相关文章:

c# - 什么时候为变量分配内存,是在声明时还是在初始化时?

使用 Numpy 进行 Python C 扩展

c - GTK+ 显示网站图标图像

c - 调用结构成员时出现段错误

无法使用堆栈分隔表达式

C代码堆栈损坏更改变量

java - HackerRank 上两个堆栈博弈的正确算法

c - 表达式无效警告

c - OSX 10.5.8 下出现 undefined symbol 错误

c - 如何从嵌入式ARM处理器中的某个物理内存分配一 block 内存?