Stack is a linear data structure that follows the LIFO (Last In First Out) principle, where it performs all operations. It performs insertion and deletion operations on the stack from only one end from the top of the stack. Inserting a new element on the top of the stack is known as push operation, and deleting a data element from the top of the stack is known as pop operation. You can perform the implementation of the stack in memory using two data structures: stack implementation using array and stack implementation using linked-list.

## Stack Implementation Using Array

In Stack implementation using arrays, it forms the stack using the arrays. All the operations regarding the stack implementation using arrays.

## Procedure for Stack Implementation Using Array

Now, you will see how to do a stack implementation using an array:

### Push Operation:

Adding an element on the top of the stack is termed a push operation. Push operation has the following two steps:

- Increment the top variable of the stack so that it can refer to the next memory location.
- Add a data element at the increment top position.

Stack data structure states an overflow condition when you try to insert an element into the stack when complete.

Algorithm of push operation:

beginĀ Ā Ā Ā if top = n Ā Ā Ā stack is full Ā Ā Ā top = top + 1 Ā Ā Ā stack(top) = data end |

Implementation of push operation:

void push( int data, int n) { If ( top == n) Ā Ā printf(āoverflowā); else { Ā top = top +1; Ā stack[top] = data; } } |

### Pop Operation:

Removing a data element from the stack data structure is called a pop operation. The pop operation has two following steps:

- The value of the top variable will be incremented by one whenever you delete an item from the stack.
- The topmost variable of the stack is stored in another variable, and then the value of the top variable will be decremented by one.
- The pop operation returns the deleted element that was stored in another variable as a result.

Stack data structure states an underflow condition when you try to delete a data element when the stack is already empty.

Algorithm of pop operation:

Begin Ā if top = 0 Ā stack is empty Ā value = stack(top) Ā top=Ā =top -1 end |

Implementation of pop operation:

int pop() { if (top == -1) { printf(āunderflow conditionā); } else { return stack[top--]; } } |

### Peek Operation:

- Peek operations involve returning the topmost data element of the stack without removing it from the stack.
- Underflow conditions may occur if you try to return the topmost element if the stack is already empty.

Algorithm of peek operation:

BeginĀ Ā if top = -1 Ā stack is empty Ā data = stack[top] Ā return data end |

Implementation of a peek operation:

int peek() { if (top == -1) { Ā Ā Ā printf(āunderflowā); Ā Ā Ā return 0; } else { Ā Ā Ā return stack[top]; } } |

## Pros and Cons of Stack Implementation Using ArrayĀ

There are various pros and cons of a stack implementation using an array, they are:

### Pros:Ā

- It requires no extra memory to store the pointers in stack implementation using an array.
- More efficient in terms of time, compared to stack implementation using linked-list.

### Cons:Ā

- The size of the stack is fixed, so it cannot increase and decrease stack implementation using an array.
- Insertion and deletion in an array are quite difficult as it stores the elements in consecutive memory locations.

With this, we have reached the end of the stack implementation using an array tutorial.

### Next Step

"Stack implementation using linked-list" can be your next topic, wherein you will learn how to implement stack using linked-list structure.

If you are interested in building a career in the field of software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. The Post Graduate Program in Full Stack Development offered in collaboration with Caltech CTME is the most comprehensive program in the portfolio, designed to help current and aspiring software developers to gain a complete understanding of all the in-demand front-end and back-end stack of software tools, languages, techniques and critical skills. Do check it out. It could prove to be a game-changer for your career.Ā

If you have any questions regarding the "stack implementation using array" tutorial, please feel free to mention them in the comment section below. We will resolve your problems as soon as possible.Ā

Happy learning!