Sunday November 3rd, 2019

Stack

By Ebubekir Sezer

Hello, Stack is an Abstract Data Type. Stacks are defined like the Last in First Out (LIFO) or First in Last Out(FILO). Stack can be used with the Arrays and Linked Lists. In this example, I will try to use Stack with the Array. Stack has 3 important functions and these are;

  • Push : To add item, we use push function.
  • Pop : To delete item, we use pop function.
  • Top : To show top value, we use top function.

After the definitions, we can start the coding. Firstly, i define a struct and inside of the this struct, i keep the size, top and array. I define the struct name as Stack.

//Representing the Stack
typedef struct s{
	int size;
	int top;
	int *array;
}stack;

For the create a stack, I define a function and this function get the size value and return a stack. In the function using with the malloc function, i create an array for the stack and the size of this array will be the size which we get with the function.

//Creating a stack
stack *createStack(int size){
	stack *mystack = (stack *)malloc(sizeof(stack));
	mystack->size = size;
	mystack->top = 0;
	mystack->array =(int *)malloc(sizeof(int)*size);
	return mystack;
}

For the add data to the stack, I need to define push function. Push function will not return anything for that i make the function type void. In the function, we need to check some situations like if array is full, we need to make the size of the array bigger. For the make bigger array, i create a new array and then i copy the all values of the my old array and delete from the memor using free function. I make the myfirst array equal to the other array and then i make the size times two.

//Adding item to the stack
void push(stack *mystack,int data){
	if(mystack->top >= mystack->size-1){
		int *array2 = (int *)malloc(sizeof(int)*mystack->size*2);
		
		for(int i =0;i<mystack->size;i++){
			array2[i]= mystack->array[i];
		}
		free(mystack->array);
		mystack->array = array2;
		mystack->size *=2;
	}
	mystack->array[mystack->top++] = data;
}

For the delete data from stack, I need to define Pop function. Pop function will return a interger value for that reason i make the type of the function int. In the function, we need to check some conditions like if the top value of the array fourfold smaller than the size of the array, we need to decrease the size of the array.  For that, I create a new array and then keep the all values of the first array and then i decrease the size of the array.

//Deleting item from stack
int pop(stack *mystack){
	if(mystack->top <= mystack->size/4){
		int *array2 = (int *)malloc(sizeof(int)*mystack->size/2);
		for(int i=0;i<mystack->top;i++){
			array2[i]=mystack->array[i];
		}
		free(mystack->array);
		mystack->array = array2;
		mystack->size /=2;
	}
	return mystack->array[--mystack->top];
}

For the learn top value number of the array, I create a function which return topvalue.

//Returing top value
int topValue(stack *mystack){
	int top = mystack->top;
	return top;
}

After the making all of the above process, I want to see the values of the stack. For that, I create a function which shows the value of the array.

//Showing items of the Stack
void show(stack *mystack){
	printf("Size : %d\n",mystack->size);
	printf("Top Value : %d\n",topValue(mystack));
	for(int i =0 ;i<mystack->top;i++){
		printf("%d ",mystack->array[i]);
	}
	printf("\n\n");
}

Now everything is ready, we just need to write main function and create stacks.

int main(){
	stack *s1= createStack(2);
	
	stack *s2 = createStack(4);
	
	push(s1,10);
	push(s1,20);
	push(s1,30);
	push(s1,40);
	show(s1);
	pop(s1);
	show(s1);
	push(s2,1);
	push(s2,2);
	push(s2,3);
	push(s2,4);
	show(s2);
}

For your questions, you can reach me via e-mail or comments.