小编Gar*_*way的帖子

C中的简单堆栈程序

我在OS类中,我必须编写一个简单的堆栈程序(main函数只是确定用户要求你做什么).如果这不是必须在C中,我会在很久以前完成这个,但因为我不擅长C编码,它有"一个错误"......到目前为止的错误是它只是继续"流行"相同的价值.(它实际上并没有弹出任何东西).我认为这是因为我不明白结构和指针是如何工作的.或者这是一个不那么明显的编码错误?

#include <stdio.h>

struct node {
    int data;
    struct node *next;
    struct node *prev;
} first;

void push(int);
void pop();

int main(void)
{
    int command = 0;

    while (command != 3)
    {
        printf("Enter your choice:\n1) Push integer\n2) Pop Integer\n3) Quit.\n");
        scanf("%d",&command);
        if (command == 1)
        {
            // push
            int num;
            scanf("%d",&num);
            push(num);
        }
        else
        {
            if (command == 2)
            {
                pop();
            }
            else
            {
                if (command != 3)
                {
                    printf("Command not understood.\n");
                }
            }
        }
    }

    return 0;
}

void …
Run Code Online (Sandbox Code Playgroud)

c stack

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

stack ×1