1

I am getting below error message. Could not able to solve it. Googled a lot. Finally thought of to put it here.

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 Answers 3

5

remove typedef and all will be fine.

Sign up to request clarification or add additional context in comments.

Comments

3

You don't want a new type:

typedef struct stack *node, *top;

Instead, you want a new variable:

struct stack *node, *top;

Comments

1

You cannot combine variable declarations with typedef. If you would like to create an alias for struct stack and call it simply stack, modify your code as follows:

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

3 Comments

Wow, that's confusing. In the last line does stack refer to struct stack or to typedef struct stack stack ? Maybe another name for the alias would be clearer.
@Deverill It's the same thing, but on the last line struct stack is referenced by its typedef-ed alias, not by its tag. Using aliases identical to tags is a relatively common practice.
Maybe I'm just getting old and easily confused. :) Thanks for the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.