0

There are huge amounts of error, Seriously what's wrong ? I tried using it wihout typedef but what's the problem? can anyone help me debug this please?

struct node {
    int info;
    struct node *link;
};

int main (void)
{
    int choice;
    struct node *top;
    top = NULL;
    while (1) {
        printf("1.Push\n");
        printf("2.Pop\n");
        printf("3.Display\n");
        printf("4.Quit\n");
        printf("Enter your choice : ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                push();
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(1);
            default:
                printf("Wrong choice\n");
        }
    }
    return 0;
}

void push (void)
{
    struct node *tmp;
    int pushed_item;
    tmp = malloc(sizeof(struct node));
    printf("Input the new value to be pushed on the stack : ");
    scanf("%d", &pushed_item);
    tmp->info = pushed_item;
    tmp->link = top;
    top = tmp;
}

void pop (void)
{
    struct node *tmp;
    if (top == NULL)
        printf("Stack is empty\n");
    else {
        tmp = top;
        printf("Popped item is %d\n", tmp->info);
        top = top->link;
        free(tmp);
    }
}

void display (void)
{
    struct node *ptr;
    ptr = top;
    if (top == NULL)
     printf("Stack is empty\n");
    else {
        printf("Stack elements :\n");
        while (ptr != NULL) {
            printf("%d\n", ptr->info);
            ptr = ptr->link;
        }
    }
}
3
  • 2
    Can you format your code properly, please? Commented Apr 6, 2011 at 23:15
  • 1
    And what are the errors you're getting? Please post the precise error messages. Commented Apr 6, 2011 at 23:15
  • Please post the compiler you are using, the compiler options you are using, and the specific error output that you are seeing. Commented Apr 7, 2011 at 0:01

2 Answers 2

1

First off, the main function is going to have to be below the functions it calls. Secondly, you need to #import <stdio.h> to use printf. Thirdly, top is not a global variable so you can't just use it inside the display function.

Work from there.

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

3 Comments

#import or #include - too much Java there buddy!
Also, if his code is structured correctly, his functions are in a .h file, and this is just his code file. (Note, that's an if. And by the looks of the code, a big if.)
The main function does not have to be below the functions it calls - just prototype the other functions up above (should be in a header file generally).
1

Try putting this snippet at the top of your file:

#include <stdio.h>
#include <stdlib.h>

void push(void);
void pop(void);
void display(void);

struct node* top; // good catch by Borealid

1 Comment

You will also need #include<stdlib.h> for the exit and free.

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.