Program to push value and pop in stack.

program:-


#include<stdio.h>
struct arraystack
{
int top;
int capacity;
int *array;
};

// function for cerate stack

struct arraystack *creatstack(int cap)
{
struct arraystack *stack;
stack=malloc(sizeof(struct arraystack));
stack->capacity=cap;
stack->top=-1;
stack->array=malloc(sizeof(int)*stack->capacity);
return(stack);
}

// function for checking stack is full or not

int isfull(struct arraystack *stack)
{
if(stack->top==stack->capacity1)
return(1);
else
return(0);
}

// funtion for checking stack is empty or not

int isempty(struct arraystack *stack)
{
if(stack->top==-1)
return(1);
else
return(0);
}

// function for push element in to stack

void push(struct arraystack *stack,int item)
{
if(!isfull(stack))
{
stack->top++;
stack->array[stack->top]=item;
}

else
printf(“stack is fulln”);
}

// function for pop element from stack

int pop(struct arraystack *stack)
{
int item;
if(!isempty(stack))
{
item=stack->array[stack->top];
stack->top–;
return(item);
}
else
return(-1);
}
void main()
{
int ch,item;
struct arraystack *stack;
stack=creatstack(4);
while(1)
{
system(“cls”);
printf(“pressn”);
printf(“1. pushn”);
printf(“2. popn”);
printf(“3. Exitn”);
printf(“Enter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“enter a numbern”);
scanf(“%d”,&item);
push(stack,item);
break;
case 2:
item=pop(stack);
if(item==-1)
printf(“stack is emptyn”);
else
printf(“%dn”,item);
break;
case 3:
exit(0);
}
getch();
}
}


Leave a Reply

Your email address will not be published.