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]);
}
}
}
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