0

I tried doing a program for implementing the various operations in stack such as pop,push,traverse,peek etc. But I used static memory allocation. How can I implement the program using dynamic memory allocation?

(I am a beginner in programming and don't have a thorough knowledge in dynamic memory allocation).

Here is my program which I used static memory allocation:

#include<stdio.h>
#define CAPACITY 5
int stack[CAPACITY] ,top=-1;

void main()
{
    int ch,item;
    while(1)
    {
        printf("1.push\n");
        printf("2.pop\n");
        printf("3.peek \n");
        printf("4.traverse\n");
        printf("5.Quit\n");

        printf("Enter your choice :\n");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                printf("Enter the element to push :\n");
                scanf("%d",&item);
                push(item);
                break;
            case 2:
                item = pop();
                if(item==0)
                {
                    printf("stack is underflow\n");
                }
                else
                {
                    printf("popped item : %d\n",item);
                }
                break;
            case 3: peek();
            break;
            case 4: traverse();
            break;
            case 5: exit(0);
            default:printf("Invalid input \n\n");

        }
    }

}
void push(int ele)
{
    if(isfull())
    {
        printf("Stack is overflow\n");
    }
    else
    {
        top++;
        stack[top] = ele;
        printf("%d pushed \n",ele);
    }
}
int isfull()
{
    if(top==CAPACITY-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int pop()
{
    if(isempty())
    {
        return 0;
    }
    else
    {
        return stack[top--];
    }
}
int isempty()
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void peek()
{
    if(isempty())
    {
        printf("stack is underflow\n");
    }
    else
    {
        printf("peek element :%d\n",stack[top]);
    }
}
void traverse()
{
    if(isempty())
    {
        printf("stack is empty\n");
    }
    else
    {
        int i;
        printf("Stack elements are : \n");
        for(i=0;i<=top;i++)
        {
            printf("%d \n",stack[i]);
        }
    }
}
6
  • Does this answer your question? stack implementation using malloc in c [BEGINNER] Commented May 28, 2020 at 14:08
  • OT: What gives push 1, push 2, push 0, pop? Do you think that the underflow message is correct? Commented May 28, 2020 at 14:10
  • @SergeBallesta i dont really know that. I tried doing that and it isrunning fine. Commented May 28, 2020 at 14:13
  • @MichaelBianconi I am checking that. Commented May 28, 2020 at 14:13
  • 1
    I did not say there was an error. I just said that the message stack is underflow in that use case was irrelevant. Commented May 28, 2020 at 14:21

1 Answer 1

1

How can I implement the program using dynamic memory allocation?

If you want to use DMA (dynamic memory allocation) in your program, here is the modified code. Now your progarm will initialize the stack at run-time. There were some warnings in your program which I also modified.

#include<stdio.h>
#include<stdlib.h>
//#define CAPACITY 5
//int stack[CAPACITY], top=-1;
int *stack, top = -1, CAPACITY;

// Prototype of Functions
void push(int ele);
int isfull();
int pop();
int isempty();
void peek();
void traverse();

// main()
int main(void)
{
    int ch,item;
    printf("Input Capacity of the stack: ");
    scanf("%d", &CAPACITY);
    // Dynamic Memory Allocation for the stack...
    stack = (int *)malloc(CAPACITY*sizeof(int));
    while(1)
    {
        printf("1.push\n");
        printf("2.pop\n");
        printf("3.peek \n");
        printf("4.traverse\n");
        printf("5.Quit\n");

        printf("Enter your choice :\n");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                printf("Enter the element to push :\n");
                scanf("%d",&item);
                push(item);
                break;
            case 2:
                item = pop();
                if(item==0)
                {
                    printf("stack is underflow\n");
                }
                else
                {
                    printf("popped item : %d\n",item);
                }
                break;
            case 3: peek();
            break;
            case 4: traverse();
            break;
            case 5: exit(0);
            default:printf("Invalid input \n\n");

        }
    }

}

// Definition of Functions.
void push(int ele)
{
    if(isfull())
    {
        printf("Stack is overflow\n");
    }
    else
    {
        top++;
        stack[top] = ele;
        printf("%d pushed \n",ele);
    }
}
int isfull()
{
    if(top==CAPACITY-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int pop()
{
    if(isempty())
    {
        return 0;
    }
    else
    {
        return stack[top--];
    }
}
int isempty()
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void peek()
{
    if(isempty())
    {
        printf("stack is underflow\n");
    }
    else
    {
        printf("peek element :%d\n",stack[top]);
    }
}
void traverse()
{
    if(isempty())
    {
        printf("stack is empty\n");
    }
    else
    {
        int i;
        printf("Stack elements are : \n");
        for(i=0;i<=top;i++)
        {
            printf("%d \n",stack[i]);
        }
    }
}

Now, if you want explanation, then you should refer to this DMA

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

2 Comments

an integer may have a value of zero as well, so checking against "0" wouldn't mean an underflow. it's better to check size/capacity of the stack before calling pop().
@icaptan ok. Thank you

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.