在堆栈中通过引用传递

时间:2017-11-05 09:23:34

标签: c stack pass-by-reference

我正在尝试创建一个简单的程序,其中任务的想法是创建一个堆栈,该堆栈可以为数组接受一定数量的值,并能够在命令上推送和弹出值。我无法正确到达我的传球。我认为这些值甚至不会被添加到数组中。

#include <stdio.h>
#include <stdlib.h>
int push(int x, int st, int stack1[]);
int pop(int st, int stack1[]);
int main ()
{
    char stack1[10];
    int stmax = 5;
    int st = -1;
    int x;
    int choice;

        while (1) {
                printf("What would you like to do? \n1. Check Empty\n2. Check Full\n3. Push\n4. Pop\n5. show\n\n");
                scanf("%d", &choice);
        switch (choice) {
            case 1:
                if(st = -1){
                        printf("Empty\n");
                }   else    {
                        printf("Not Full\n");
                        }

                break;
            case 2:
                if(st == stmax) {
                        printf("Full\n");
                }   else    {
                        printf("Not Full\n");
                        }

                break;
            case 3:
                printf("Enter a int to add to the stack:\n");
                scanf("%d", &x);
                push(x, st, stack1);
                break;
            case 4:
                pop(st, stack1);
                printf("%d", stack1[st]);
            default:

                break;
        }
    } return(0);
}

int pop(int st, int stack1[]) {
    int data;
    data = stack1[st];
    printf("%d", data);
    return(st);
}

int push(int x, int st, int stack1[]) {
    st ++;
    stack1[st]=x;
    return(st);
}

0 个答案:

没有答案