C program to
implement stack.. (DYNAMIC MEMORY ALLOCATION)
#include<stdio.h>
#include<stdlib.h>
int MAX=1;
int top=-1;
int isempty(){
if(top==-1) return 1;
else return 0;
}
void push(int item, int *s)
{
if(top==MAX-1)
{
printf("STACK IS FULL AND STACK SIZE IS INCREASED BY 1 \n");
MAX++;
s=(int*)realloc(s,MAX);
}
s[++top]=item;
}
void pop(int s[])
{
if(isempty()) printf("no elements in d stack to pop \n");
else
{
printf("element poped out is %d",s[top]);
top--;
}
}
void display(int *s)
{
int i;
if(isempty()) printf("stack is empty \n");
else{
for(i=0;i<=top;i++)
printf("%d \n",s[i]);
}
}
void main()
{
int *s;
int item,n;
s=(int*)malloc(sizeof(MAX));
for(;;)
{
printf("select your choice \n");
printf("1.PUSH 2.POP 3.DISPLAY 4.EXIT \n");
scanf("%d",&n);
switch(n)
{
case 1: printf("enter the elemen to be inserted \n");
scanf("%d",&item);
push(item,s);
break;
case 2: printf("pop operation is selected \n");
pop(s);
break;
case 3: printf("content of the stack are \n");
display(s);
break;
case 4:exit(0); break;
}
}
}
#include<stdio.h>
#include<stdlib.h>
int MAX=1;
int top=-1;
int isempty(){
if(top==-1) return 1;
else return 0;
}
void push(int item, int *s)
{
if(top==MAX-1)
{
printf("STACK IS FULL AND STACK SIZE IS INCREASED BY 1 \n");
MAX++;
s=(int*)realloc(s,MAX);
}
s[++top]=item;
}
void pop(int s[])
{
if(isempty()) printf("no elements in d stack to pop \n");
else
{
printf("element poped out is %d",s[top]);
top--;
}
}
void display(int *s)
{
int i;
if(isempty()) printf("stack is empty \n");
else{
for(i=0;i<=top;i++)
printf("%d \n",s[i]);
}
}
void main()
{
int *s;
int item,n;
s=(int*)malloc(sizeof(MAX));
for(;;)
{
printf("select your choice \n");
printf("1.PUSH 2.POP 3.DISPLAY 4.EXIT \n");
scanf("%d",&n);
switch(n)
{
case 1: printf("enter the elemen to be inserted \n");
scanf("%d",&item);
push(item,s);
break;
case 2: printf("pop operation is selected \n");
pop(s);
break;
case 3: printf("content of the stack are \n");
display(s);
break;
case 4:exit(0); break;
}
}
}
No comments:
Post a Comment