# Operations on a Stack

There are many operations that can be used to manipulate a stack.

A stack is a data structure in which the operations are executed in constant time.

There are a number of operations that can be performed on a stack, but there are two major operations that are used to manipulate a stack.

These operations are:

• Push()
• Pop() ## Push()

• The push operation in a stack is synonymous to insertion in a data structure.
• In simpler words, inserting a new  element in a stack is known as push.
• The time complexity of the Push() operation is O(1), i.e , it occurs in constant time.

Let us understand how a push() operation works. Let us consider we have to push a given number of elements in the stack.

The elements to be inserted are: 12 , 08 , 21 , 33 , 18 , 40.

#### Step 1:

• Initially we have an empty stack.
• Top = NULL. #### Step 2:

• Push the element 12 in the stack.
• Top = 12 #### Step 3:

• Push the element 08 in the stack.
• Top = 08. #### Step 4:

• Push the element 21 in the stack.
• Top = 21 #### Step 5:

• Push the element 33 in the stack.
• Top = 33. #### Step 6:

• Push the element 18 in the stack.
• Top = 18 #### Step 7:

• Push the element 40 in the stack.
• Top = 40. #### Step 8:

• Final stack is shown as follows.
• Top = 40 ## Pop()

• The Pop operation in a stack is synonymous to deletion in a data structure.
• In simpler words, deleting an existing element from a stack is known as pop.
• The time complexity of the Pop() operation is O(1), i.e , it occurs in constant time.

Let us understand how a pop() operation works. Let us consider we have to pop a given number of elements in the stack.

The elements to be deleted from the stack are : 40 , 18 , 33.

#### Step 1:

• The first element to be deleted from the stack is 40.
• We can see the value of top points to 40.
• We perform the pop operation and the element 40 is popped out off the stack.
• The remaining elements are shown in the image.
• Top = 18. #### Step 2:

• The second element to be deleted from the stack is 18.
• We can see the value of top points to 18.
• We perform the pop operation and the element 18 is popped out off the stack.
• The remaining elements are shown in the image.
• Top = 33. #### Step 3:

• The next element to be deleted from the stack is 33.
• We can see the value of top points to 33.
• We perform the pop operation and the element 33 is popped out off the stack.
• The remaining elements are shown in the image.
• Top = 21. ## Implementation of the Operations(C – Code) of the Stack

`#include<stdio.h>#include<stdlib.h> #define size 10	//size of the stack int top_of_stack=-1,s[size];void push();void pop();void show(); void main(){	int option;		while(1)		{		printf("\nSelect from the following options- ");		printf("\n\n1.Push\n2.Pop\n3.Show\n4.Exit");		printf("\n\nEnter the operation you want to execute: ");		scanf("%d",&option);				switch(option)		{			case 1: push();					break;			case 2: pop();					break;			case 3: show();					break;			case 4: exit(0);						default: printf("\nInvalid option. Please select a Valid operation");		}	}} void push(){	int value;		if(top_of_stack == size-1)	{		printf("\nOperation failed. Stack is full.");	}	else	{		printf("\nEnter the value you want to push in the stack:");		scanf("%d",&value);		top_of_stack = top_of_stack + 1;		s[top_of_stack] = value;	}} void pop(){	if(top_of_stack == -1)	{		printf("\nOperation failed. Stack is empty");	}	else	{		printf("\nPopped element is:  %d",s[top_of_stack]);		top_of_stack=top_of_stack-1;	}} void show(){	int i;		if(top_of_stack==-1)	{		printf("\nStack is empty");	}	else	{		printf("\nElements of the stack are:\n");		for(i = top_of_stack; i>=0; --i)			printf("%d\n",s[i]);	}}`