stack_calloc




#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void display();
int *stack,*top,n;
void main()
{
int ch;
printf("Enter the size of stack:\n");
scanf("%d",&n);
stack=(int *)calloc(n,sizeof(int));
if(stack==NULL)
{
 printf("out of memory.!!");
 return;
}
 while(1)
 {
printf("1->push 2->pop 3->display 4->exit\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(1);
default: printf("please do enter valid choice:\n");
}
 }
}//main
void push()
{
int ele;
if(top==stack+n)
{
printf("stack overflow\n");
return;
}
printf("Enter the element to be inserted");
scanf("%d",&ele);
*top=ele;
top++;
}//push
void pop()
{
int ele;
if(top==stack)
{
printf("stack underflow");
return;
}
top--;
ele=*top;
printf("Deleted element is %d",ele);
}//pop
void display()
{
int *i,ele;
if(top==stack)
{
printf("no elements to display");
return;
}
top--;
for(i=top;i>=stack;i++)
{
  ele=*i;
printf("%d\n",ele);
}
}//display

No comments:

Post a Comment