Header Ads Widget

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->capacity-1)
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 full\n");
}

// 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("press\n");
printf("1. push\n");
printf("2. pop\n");
printf("3. Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter a number\n");
scanf("%d",&item);
push(stack,item);
break;
case 2:
item=pop(stack);
if(item==-1)
printf("stack is empty\n");
else
printf("%d\n",item);
break;
case 3:
exit(0);
}
getch();
}
}



Post a Comment

0 Comments