// reason why I created a structure for stack : In push operation , I have to do several things like , check for the overflow problem , insert the element to the stack if the stack in not overflowed , update the stack top. To check the over flow condition I need the size of stack and stack top.
// so I need to pass array pointer , stack top , size , all the time when I push any element to the stack , to get rid of that , create structure containing all of them together , and pass reference to the structure only whule push() and pop().
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top;
int size;
int *stackPtr;
};
struct stack * createStack(struct stack *s1,int size)
{
//struct stack *s1;
s1->top=-1;
s1->size=size;
s1->stackPtr=(int *)malloc(size*sizeof(int));
return s1;
}
int isOverflow(struct stack *s1)
{
if((s1->top)==((s1->size)-1))
{
return 1;
}
else
{
return 0;
}
}
void push(struct stack *s1,int data)
{
if(isOverflow(s1))
{
printf("stack is full \n Unable to insert \n");
}
else
{
(s1->top)=(s1->top)+1;
(s1->stackPtr[(s1->top)])=data;
}
}
isUnderflow(struct stack *s1)
{
if((s1->top)==-1)
{
return 1;
}
else
{
return 0;
}
}
int pop(struct stack *s1)
{
int data;
if(isUnderflow(s1))
{
data=0;
printf("no element to delete in the stack");
return 0;
}
else
{
data=s1->stackPtr[(s1->top)];
(s1->top)=((s1->top)-1);
return data;
}
}
printStack(struct stack *s1)
{
int i;
if((s1->top)==-1)
{
printf("stack is EMPTY");
}
else
{
for(i=0;i<=(s1->top);i++)
{
printf("-%d-",(s1->stackPtr[i]));
}
}
printf("\n");
}
int main(void)
{
int data;
struct stack *s1=(struct stack *)malloc(sizeof(struct stack));
createStack(s1,5);
return 0;
}
// so I need to pass array pointer , stack top , size , all the time when I push any element to the stack , to get rid of that , create structure containing all of them together , and pass reference to the structure only whule push() and pop().
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top;
int size;
int *stackPtr;
};
struct stack * createStack(struct stack *s1,int size)
{
//struct stack *s1;
s1->top=-1;
s1->size=size;
s1->stackPtr=(int *)malloc(size*sizeof(int));
return s1;
}
int isOverflow(struct stack *s1)
{
if((s1->top)==((s1->size)-1))
{
return 1;
}
else
{
return 0;
}
}
void push(struct stack *s1,int data)
{
if(isOverflow(s1))
{
printf("stack is full \n Unable to insert \n");
}
else
{
(s1->top)=(s1->top)+1;
(s1->stackPtr[(s1->top)])=data;
}
}
isUnderflow(struct stack *s1)
{
if((s1->top)==-1)
{
return 1;
}
else
{
return 0;
}
}
int pop(struct stack *s1)
{
int data;
if(isUnderflow(s1))
{
data=0;
printf("no element to delete in the stack");
return 0;
}
else
{
data=s1->stackPtr[(s1->top)];
(s1->top)=((s1->top)-1);
return data;
}
}
printStack(struct stack *s1)
{
int i;
if((s1->top)==-1)
{
printf("stack is EMPTY");
}
else
{
for(i=0;i<=(s1->top);i++)
{
printf("-%d-",(s1->stackPtr[i]));
}
}
printf("\n");
}
int main(void)
{
int data;
struct stack *s1=(struct stack *)malloc(sizeof(struct stack));
createStack(s1,5);
return 0;
}
No comments:
Post a Comment