通过链表实现Stack

时间:2012-04-24 13:44:53

标签: c

我收到以下错误消息。 无法解决它。谷歌搜索了很多。最后想到把它放在这里。

enter image description here

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int stop;
struct stack
{
  int data;
  struct stack *next;
};
typedef struct stack *node, *top;
//typedef struct stack *top;
void push()
{
    int i;
    struct stack *x;
    x = malloc(sizeof(struct stack));
    printf("\n Enter the element your want to insert");
    scanf("%d", &i);
    x->data = i;
    x->next = top;
    top = x;
}
void pop()
{
    int i;
    if(top == NULL)
    {
        printf("\nStack is empty\n");
    }
    else{
    i = top->data;
    free(top);
    top = top->next;

    }
}
void display()
{
    if(node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }

}
int main()
{
    int ch;
    while(1)
    {
        printf("\nEnter your option \n1. Insert(Push) \n2. Delete(Pop) \n3. Display  : \n");
        scanf("%d", &ch);
        switch(ch)
        {
            case 1:
                    push();
                    break;
            case 2:
                    pop();
                    break;
            case 3:
                    display();
                    break;
            default:
                    printf("Invalid Entery, Try Again");
        }
    }
return 0;
}

3 个答案:

答案 0 :(得分:5)

删除typedef,一切都会好的。

答案 1 :(得分:3)

您不需要新类型:

typedef struct stack *node, *top;

相反,您需要一个新变量:

struct stack *node, *top;

答案 2 :(得分:1)

您无法将变量声明与typedef结合使用。如果您要为struct stack创建别名并简单地将其命名为stack,请按以下步骤修改您的代码:

struct stack {
    int data;
    struct stack *next;
};
typedef struct stack stack;
stack *node, *top;