-1

I am trying to write a code to depict all the basic operations of stack, but as soon as I compile my code and run it. It immediately stops working.

I am getting no compilation error in my IDE (CodeBlocks). How do I know what is the error in my code?

Can I also somehow change the settings in CodeBlocks so as to also display such runtime errors?

What is the error in my code below? Is it the use of the arrow operator (->) one after the other? Thank You.

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

typedef struct node
{
    int data;
    struct node *prev;
}node;

typedef struct stack
{
    node *top;
}stack;

int count=0;

void push(stack *x, int num)
{
    node *temp;
    if(x->top == NULL)
    {
        temp=(node *)malloc(sizeof(node *));
        temp->data=num;
        temp->prev=NULL;
        x->top=temp;
    }
    else
    {
        temp=(node *)malloc(sizeof(node *));
        temp->data=num;
        temp->prev=x->top;
        x->top=temp;
    }
    count++;
    return;
}

int pop(stack *x)
{
    node *temp;
    int m;
    if(x->top == NULL)
    {
        printf("Error:The stack is empty\n");
        return;
    }
    else
    {
        m=(x->top)->data;
        temp=(x->top)->prev;
        x->top=temp;
    }
    count--;
    return m;
}
int main()
{
    int k=1, n, num;
    stack *myStack;
    myStack->top = NULL;
    while(k)
    {
        printf("Enter the operation you want to perform\n");
        printf("1.PUSH\n");
        printf("2.POP\n");
        printf("3.TOP\n");
        printf("4.STACK COUNT\n");
        scanf("%d", &n);
        switch(n)
        {
            case 1:
                printf("Enter the number you want to push to the stack\n");
                scanf("%d", &num);
                push(myStack, num);
                break;
            case 2:
                printf("The popped element is %d\n", pop(myStack));
                break;
            case 3:
                printf("The topmost element of the stack is %d\n", (myStack->top)->data);
                break;
            case 4:
                printf("The number of elements in the stack are %d\n", count);
                break;
            default:
                printf("Invalid request\n");
                break;
        }
        printf("Do you wish to perform another operation?(1/0)");
        scanf("%d", &k);
    }
    return 0;
}
Karan Singh
  • 1,114
  • 1
  • 13
  • 30

3 Answers3

1

The problem with your code is that it does not allocate any memory to the structure.

    stack *myStack;
    myStack->top = NULL;

As long as you don't specify it, the operating system does not know how much memory it needs to set aside, or if it needs to set aside memory at all. It is all decided during runtime when the user decides if he/she wants to do it or not.

    printf("Do you wish to perform another operation?(1/0)");
    scanf("%d", &k);

Normally, when you use a variable or a pointer to a variable, this job of memory allocation is handled by your compiler and the OS because the size of that variable is known to them, but when you use a stack (implemented using linked list) you need memory on the fly because the user determines how much data he/she wants to store or delete:

while(k)
{
    printf("Enter the operation you want to perform\n");
    printf("1.PUSH\n");
    printf("2.POP\n");
    printf("3.TOP\n");
    printf("4.STACK COUNT\n");
    scanf("%d", &n);
    switch(n)

Hence the need for dynamic memory allocation. See it as gaining a greater control over your memory. It gives you the power to decide what you want to do with your memory. You can ask the operating system for any sized piece of memory you want and keep it with you for whatever length of time you fancy.

I think this answers yours 'why dynamic memory' question. Now the problem with your code:

case 1:
            printf("Enter the number you want to push to the stack\n");
            scanf("%d", &num);
            push(myStack, num);

There's no need of passing any address of any structure to the push function. The memory allocation is handled in your push function. So you only need:

push(num);

Which means that you need to change the function declaration and the entire definition with it. The function declaration should look as such:

node* push(int);

Now, modify the function definition as required.

Also, to help you in finding out the runtime errors you could take the help of your IDE's built-in debugger. So learn how to use it.

Also I think it might help if you read a certain chapter on dynamic memory allocation from a good book your friend recommends :)

Nityesh Agarwal
  • 464
  • 2
  • 5
  • 18
0
stack *myStack;
myStack->top = NULL;

Here, myStack is not pointing to any object of type stack. It is just an uninitialized pointer. Hence, when you try to set the value for top using -> a runtime error is seen.

You can begin with initializing myStack like this:

stack *myStack = malloc (sizeof(stack));

Additionally, there is a memory leak in your pop function:

m=(x->top)->data;
temp=(x->top)->prev;
x->top=temp; // now x->top is pointing to x->top->prev

You have leaked the memory pointing to original node * to which myStack->top was pointing to when the pop function was called.

Rishi
  • 1,387
  • 10
  • 14
  • Why do I even need to allocate memory to stack? Why dynamic memory? – Karan Singh Feb 19 '17 at 09:25
  • You are right, you do not need dynamic memory. However, I have answered based on the code snippet where you have used `stack *myStack`. `myStack` is a pointer here. You can create `stack myStack` and pass address of `myStack` in your push and pop functions. – Rishi Feb 19 '17 at 09:28
0

please have look, i think problem is resolved.

`

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

typedef struct node
{
    int data;
    struct node *prev;
}node;

typedef struct stack
{
    node *top;
};

// struct stack myStack=NULL;

int count=0;

void push(stack *x, int num)
{
    node *temp;
    if(x->top == NULL)
    {
        temp=(node *)malloc(sizeof(node *));
        temp->data=num;
        temp->prev=NULL;
        x->top=temp;
    }
    else
    {
        temp=(node *)malloc(sizeof(node *));
        temp->data=num;
        temp->prev=x->top;
        x->top=temp;
    }
    count++;
    return;
}

int pop(stack *x)
{
    node *temp;
    int m;
    if(x->top == NULL)
    {
        printf("Error:The stack is empty\n");
        return 0;
    }
    else
    {
        m=(x->top)->data;
        temp=(x->top)->prev;
        x->top=temp;
    }
    count--;
    return m;
}
int main()
{
    int k=1, n, num;
    printf("h");

    printf("h");
    stack *myStack = (struct stack*)malloc (sizeof(stack));    //myStack->top = NULL;
    while(1)
    {
        printf("Enter the operation you want to perform\n");
        printf("1.PUSH\n");
        printf("2.POP\n");
        printf("3.TOP\n");
        printf("4.STACK COUNT\n");
        scanf("%d", &n);
        switch(n)
        {
            case 1:
                printf("Enter the number you want to push to the stack\n");
                scanf("%d", &num);
                push(myStack, num);
                break;
            case 2:
                printf("The popped element is %d\n", pop(myStack));
                break;
            case 3:
                printf("The topmost element of the stack is %d\n", (myStack->top)->data);
                break;
            case 4:
                printf("The number of elements in the stack are %d\n", count);
                break;
            default:
                printf("Invalid request\n");
                break;
        }
        printf("Do you wish to perform another operation?(1/0)");
        scanf("%d", &k);
    }
    return 0;
}

`