CREATE OWN LIBRARY

Stack Push and Pop Using Array

Back to Programming

Description

The stack is an abstract data type. It is a linear data structure and has a particular order of performing the operations. Generally, the stack uses the LIFO approach i.e. Last In First Out approach. The element that is inserted at the last will be deleted at the first. There are various real-life examples of the stack. 

 

The stack of plates in the kitchen can be a very simple and common example of a stack data structure. The plate which is placed at the top (i.e. which is put on the stack at last) will be taken first. The bottom-most plate which has been kept first will remain there and will be taken out at the last. The insertion of any element into the stack is called ‘Push’ and the deletion of an element is called ‘pop’.

 

 

Here, the array is used to implement the stack data structure. An array is also a linear data structure. First, the maximum number of elements of the stack is taken to check the boundary conditions- overflow or underflow

 

When the stack is full and someone is trying to insert an element into the stack then this condition is known as stack overflow.

 

Similarly, when the stack is empty and someone is trying to delete an element from the stack then this condition is known as stack underflow.

 

Let the stack can contain a maximum of 3 elements.

 

So, the stack can be represented as:

 

 

The array representation is: 

 

 

Initially top=-1

 

Now, suppose the first element to be inserted into the stack is 10. The stack will be:

 

 

For implementing this using an array first the top will be incremented by 1. Therefore, top=-1+1=0.

 

Now the element ‘10’ will be inserted at stack[top]=stack[0]. After inserting the array will be:

 

 

For the second element, again the same process will be followed. Suppose the element to be inserted is 20.

 

 

For implementing in the array, the top will be incremented by 1. Therefore, top = 0 + 1 = 1.

 

Now the element ‘20’ will be inserted at stack[top] = stack[1]. After inserting the array will be:

 

 

Similarly, the elements can be inserted into the stack as well as the array. When the stack will be full it will show the “stack overflow” error while trying to insert an element.

 

Now, suppose we want to pop the element from the stack, first the element ‘20’ will be popped out as it was inserted last.

 

 

The array element cannot be deleted. Therefore, for implementing this using array, the top will be decremented by 1.

 

 

When the array will be displayed, it will be displayed from index 0 to top which will help us to delete the elements virtually from the array.

Algorithm

INPUT: The elements of the stack
OUTPUT: The stack after pushing or popping the elements
PROCESS:
Step 1: Declare global variable ‘top’
	Set top<- -1
Step 2: [Function ‘push()’]
	If top=n-1 then
		Print "Stack Overflow"
		return
	[End of ‘if’]
	Print "Enter the data to be pushed: "
	Read x
	Set top<-top+1
	Set a[top]<-x
[End of function ‘push()’]
Step 3: [Function ‘pop()’]
	If top=-1 then
		Print "Stack Underflow"
		return
	[End of ‘if’]
	Set x<-a[top]
	Set top<-top-1
[End of function ‘pop()’]
Step 4: [Function ‘display()’]
	If top=-1 then
		Print "No Element"
		return
	[End of ‘if’]
	Print "The elements of the stack are: "
	For i=0 to top repeat
		Print a[i]
	[End of ‘for’ loop]
[End of function ‘display()’]
Step 5: [function ‘main()’]
	Print "Enter the number of elements: "
	Read n 
	While 1 repeat 
		Print "1.PUSH 
               2.POP
               3.DISPLAY
               4.EXIT
           Enter the choice: "
		Read ch
		Switch to ch
			case 1- call the function push()
					break
			case 2-call the function pop()
					break
			case 3- call the function display()
					break
			case 4- return
			default- print "Wrong Choice"
		[End of ‘switch case’]
	[End of ‘while’ loop]
[End of function ‘main()’]
Step 6: Stop.

Code

CHARACTERISTICS:

1. The insertion or deletion of any item is done from the same end of the stack.

 

2. The stack follows the principle LIFO i.e. Last In First Out.

 

ADVANTAGES:

1. Whenever any function is called, the local variables are stored in the stack.

 

2. For a recursive function call, the stack is used.

 

3. It helps to allocate and deallocate the memory spaces.

 

4. It is used as temporary memory storage.

 

5. It is a contiguous memory allocation that is similar to the array; it makes the user understand the use of the stack easily.

 

6. It automatically cleans the object.

 

7. It is not to get corrupted easily.

 

DISADVANTAGES:

1. The memory of the stack is limited.

 

2. It is not possible to access the stack randomly.

 

3. There is a risk of stack overflow if too many objects are created in the stack.

 

4. The storage of variables may be overwritten, which may lead to undefined behavior of the functions or programs.

 

APPLICATIONS:

1. The stack is used to convert the expressions from infix to prefix, infix to postfix, and vice versa. Similarly, from prefix to postfix and vice versa.

 

2. It is used to implement the backtracking procedure in some algorithms.

 

3. Stores the address of instruction while implementing recursive functions or for calling one function from another function.

 

TIME COMPLEXITY:

The time complexity of push and pop operations into the stack is O(1). For push or pop, only the ‘top’ of the stack is accessed, there is no need to access the whole stack; therefore, it only needs constant time.