尝试在C中为赋值实现基于数组的堆栈

时间:2016-03-04 21:02:23

标签: c arrays stack

我正在尝试在C中实现基于数组的堆栈。我的堆栈应该有两个参数,top(数组中顶部元素的数量)和数组(数组本身)。我的实现如下。

typedef struct
    {
    char array[20];
    int top;
    }
    stack;

stack mystack;

int Push(char,stack);
char Pop(stack);
char Top(stack);
int isFull(stack);

char input;
char save;

void main()
    {
    mystack.top = -1;

    printf("Please input the characters you would like in your stack \n     

    while(input != '^')
            {
            Push( (scanf("%c",&input)) , mystack );

            if (isFull(mystack) == 1)
            printf("Your Stack is full, please input '^'\n");
            }

    char junk;
    scanf("enter any character to continue %c",&junk);

    while(mystack.top != -1)
            {
            printf("%c \n",Pop(mystack));
            }
    scanf("enter any character to terminate the program",&junk);
    }

int Push(char charpush,stack stackpush)
    {
    if(stackpush.top >=20 )
            return -1;
    else
            {
            stackpush.array[stackpush.top + 1] = charpush;
            stackpush.top = stackpush.top +1;
            return 0;
            }
    }
char Pop(stack stackpop)
    {
    if (stackpop.top != -1)
            {
            save = stackpop.array[stackpop.top];
            stackpop.top = stackpop.top-1;
            return save;
            }
    }
char Top(stack stacktop)
    {
    if (stacktop.top != -1)
            return stacktop.array[stacktop.top];
    }
int isFull(stack stackisfull)
    {
    if (stackisfull.top = -1)
            return 0;
    else if (stackisfull.top >= 20)
            return 1;
    else return -1;
    }

目前我的程序接受来自用户的字符,但是当输入“^”时程序会自动终止。它不显示堆栈,如果字符通过输入并且堆栈已满,则它不会执行任何操作。

如果我需要更具体或需要任何进一步的信息,请告诉我。

2 个答案:

答案 0 :(得分:2)

你有很多问题要纠正......

  • 你误解了scanf

    • 它不会返回它所读的内容
    • 它不接受提示
    • 从终端阅读时,在按下返回之前不会“看到”任何内容
  • 当堆栈为空时,您的方法Pop()会返回什么?

  • 当堆栈为空时,您的方法Top()会返回什么?

  • 你为什么写while(mystack.top != -1)?编写while (!isEmpty(mystack))然后编写isEmpty方法会更有意义吗?

  • 您没有初始化input - 您是否知道开头的内容是什么?

  • 你的缩进“计划”让我头疼。 :)

答案 1 :(得分:1)

除了@John Hascall提到的要点之外,C是一种按值传递的语言。每个函数调用的含义您提供的参数在范围内是本地的,请参阅this其他stackoverflow post。

拥有全局mystack变量(不是最佳实践)将起作用,但不会影响您当前使用它的方式。通过将mystack传递给每个函数,更改仅在所使用的参数上可见,从而破坏了具有该全局的目的。

我已经对缩进和逻辑错误进行了少量编辑,但主要的变化是编辑你的功能而不采取"堆栈"参数并使用你的全局:

#include <stdio.h>

typedef struct
{
    char array[20];
    int top;
} stack;

stack mystack; // your global
int Push(char); // remove "stack" arg for each stack-utility function
char Pop(void);
char Top(void);
int isFull(void);

char input;
char save;

// main as returning int and excepting argc/*argv[]
int main(int argc, char *argv[])
{
    mystack.top = -1;

    printf("Please input the characters you would like in your stack \n"); 

    while(input != '^')
    {
        // by including scanf inside the function call return is passed
        scanf("%c",  &input);
        Push( input );

        if (isFull() == 1)
            printf("Your Stack is full, please input '^'\n");
    }

    char junk;
    // scanf will not print
    printf("enter any character to continue\n");
    scanf("%c",&junk);

    while(mystack.top != -1)
    {
        printf("%c \n",Pop());
    }
    // same as last comment
    printf("enter any character to terminate the program\n");
    scanf("%c",&junk);
}

int Push(char charpush)
{
    if(mystack.top >=20 )
        return -1;
    else
    {
        mystack.array[mystack.top + 1] = charpush;
        mystack.top = mystack.top +1;
        return 0;
    }
}

char Pop(void)
{
    if (mystack.top != -1)
    {
        save = mystack.array[mystack.top];
        mystack.top = mystack.top-1;
        return save;
    }
        // return has to match declaration type
    return 0;
}

char Top(void)
{
    if (mystack.top != -1)
        return mystack.array[mystack.top];
    // same as last comment
    return 0;
}

int isFull(void)
{
    // you were assigning not comparing
    if (mystack.top == -1)
        return 0;
    else if (mystack.top >= 20)
        return 1;
    else return -1;
}