Thursday, February 5, 2015

Implement stack using Dynamic Array

// 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;
}

Implement stack Using Singly Linked List.

// I am going to implement stack using linked list.
// as I am using linked list , there is not required to check the overflow condition in push operation , becaues I I can grow the stack to the size of my RAM , there is no limitation.

// the cons is that , it requires extra memory to store the link field of the linked list.

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *link;
};

struct node *createNode(struct node *head,int data)  // will create a node and return the pointer to that new node.
{
struct node *p=NULL;

p=(struct node*)malloc(sizeof(struct node));
p->data=data;
p->link=NULL;

return p;
}

// In below function I am passing the pointer to pointer to the structure , because in the defination of the push() operation it do not return any thing , and while puch I have to change the head pointer to the new node , so to reflect the change in main() , I am passing pointer to pointer to structure.

// In linked list implementation of stack , always remember that we have to we have to insert the element to the start of the list and delete the eleemnt from the start of the list , otherwise while deletion , for each deletion we have to traverse the entire list.

void push(struct node **ptrToHead,int data)
{
struct node *head,*p=NULL;
head=*ptrToHead;

p=createNode(head,data);
p->link=head;
head=p;


*ptrToHead=head;
}


int isUnderflow(struct node **ptrToHead)
{
struct node *head=NULL;
head=*ptrToHead;

if(head==NULL)
{
return 1;
}
else
{
return 0;
}
}

// pop() will return the data value it just delete.

int pop(struct node **ptrToHead)
{
struct node *head,*temp=NULL;
int data;

head=*ptrToHead;

if(isUnderflow(ptrToHead))
{
return 0;
}
else
{
temp=head;
head=head->link;
data=temp->data;
free(temp);
temp=NULL;
*ptrToHead=head;
return data;
}


}

void printStack(struct node *head)
{
struct node *temp=head;

if(temp==NULL)
printf("no element to print in the list\n");
else
{
while(1)
{
printf("-%d-",(temp->data));
temp=temp->link;
if(temp==NULL)
{
break;
}
}

printf("\n");

}
}

int main(void)
{
struct node *head=NULL;
int data;

push(&head,5);
push(&head,4);
push(&head,3);
push(&head,2);
push(&head,1);

printStack(head);

return 0;
}

Implement stack using Array.

// 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;
}