Creating of the stack using array

//Creating of stack using ArrayStack
#include<stdio.h>
#include<conio.h>
struct ArrayStack
{
int top, capacity;
int *array;
};
struct ArrayStack* createStack(int cap)
{
struct ArrayStack *stack;
stack=malloc(sizeof(struct ArrayStack));
stack->top=-1;
stack->capacity=cap;
stack->array=malloc(sizeof(int)*stack->capacity);
return(stack);
}
int IsFull(struct ArrayStack *stack)
{
if(stack->top==stack->capacity)
return(1);
else
return(0);
}
int IsEmpty(struct ArrayStack *stack)
{
if(stack->top==-1)
return(1);
else
return(0);
}
void Push(struct ArrayStack *stack,int item)
{
if(!IsFull(stack))
{
stack->top++;
stack->array[stack->top]=item;
}
}
int Pop(struct ArrayStack *stack)
{
int item;
if(!IsEmpty(stack))
{
item=stack->array[stack->top];
stack->top--;
return(item);
}
else
return(-1);
}
main()
{
int choice, item, c;
struct ArrayStack *stack;
clrscr();
printf("\nEnter the capacity of Stack : ");
scanf("%d",&c);
stack=createStack(c);
while(1)
{
printf("\n1-PUSH");
printf("\n2-POP");
printf("\n3-EXIT");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter a number for PUSH : ");
scanf("%d",&item);
Push(stack,item);
break;
case 2:
item=Pop(stack);
if(item==-1)
printf("Stack is Empty.");
else
printf("Popped Value is : %d",item);
break;
case 3:
return(0);
}
}
getch();
}
//If you want its output just copy and paste in MsDos
//If you have any query or any doubt in any step so please comment below.
 

Comments

Popular posts from this blog

JAVA ARCHITECTURE

Introduction Of JAVA

OOPs Concepts