// 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().
// Dynamic array means : if my stack is full then I will increase the size of array , and will able to continue the insert operation.
// so for each time my arrray is full I will increase the size of my array to twice the old array size.
// and while deletion , if number of element is array is half of the size of array then I will reduce the size to half of the old array size.
// that means after performing deletion operation if[(stackTop+1) < (size/2) ] then reduce the array size by half.
#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)
{
//struct stack *s1=*s11;
int i=0;
if(isOverflow(s1))
{
//printf("stack is full \n Unable to insert \n");
//(s1->size)=(s1->size)*2;
int *newArrPtr=(int *)malloc(2*(s1->size)*sizeof(int));
for(i=0;i<=(s1->top);i++)
{
newArrPtr[i]=(s1->stackPtr[i]);
}
(s1->stackPtr)=newArrPtr;
(s1->size)=2*(s1->size);
(s1->top)=(s1->top)+1;
(s1->stackPtr[(s1->top)])=data;
}
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,i=0;
int *newArrPtr=NULL;
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);
if((s1->top)<((s1->size)/2)) // reduce the array size by half
{
//printf("I am inside");
newArrPtr=malloc(((s1->size)/2)*sizeof(int));
for(i=0;i<=(s1->top);i++)
{
newArrPtr[i]=(s1->stackPtr[i]);
}
(s1->size)=((s1->size)/2);
s1->stackPtr=newArrPtr;
}
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,1);
push(s1,5);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,4);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,2);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,6);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,8);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,10);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,11);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,12);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,13);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,14);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,13);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,14);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
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().
// Dynamic array means : if my stack is full then I will increase the size of array , and will able to continue the insert operation.
// so for each time my arrray is full I will increase the size of my array to twice the old array size.
// and while deletion , if number of element is array is half of the size of array then I will reduce the size to half of the old array size.
// that means after performing deletion operation if[(stackTop+1) < (size/2) ] then reduce the array size by half.
#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)
{
//struct stack *s1=*s11;
int i=0;
if(isOverflow(s1))
{
//printf("stack is full \n Unable to insert \n");
//(s1->size)=(s1->size)*2;
int *newArrPtr=(int *)malloc(2*(s1->size)*sizeof(int));
for(i=0;i<=(s1->top);i++)
{
newArrPtr[i]=(s1->stackPtr[i]);
}
(s1->stackPtr)=newArrPtr;
(s1->size)=2*(s1->size);
(s1->top)=(s1->top)+1;
(s1->stackPtr[(s1->top)])=data;
}
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,i=0;
int *newArrPtr=NULL;
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);
if((s1->top)<((s1->size)/2)) // reduce the array size by half
{
//printf("I am inside");
newArrPtr=malloc(((s1->size)/2)*sizeof(int));
for(i=0;i<=(s1->top);i++)
{
newArrPtr[i]=(s1->stackPtr[i]);
}
(s1->size)=((s1->size)/2);
s1->stackPtr=newArrPtr;
}
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,1);
push(s1,5);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,4);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,2);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,6);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,8);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,10);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,11);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,12);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,13);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,14);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
printf("\n %d deleted\n",pop(s1));
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,13);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
push(s1,14);
printStack(s1);
printf("\nstack size is %d\n",(s1->size));
return 0;
}