Tutorial Playlist

Data Structure Tutorial

Overview

Arrays in Data Structures: A Guide With Examples

Lesson - 1

All You Need to Know About Two-Dimensional Arrays

Lesson - 2

All You Need to Know About a Linked List in a Data Structure

Lesson - 3

The Complete Guide to Implement a Singly Linked List

Lesson - 4

The Ultimate Guide to Implement a Doubly Linked List

Lesson - 5

The Fundamentals for Understanding Circular Linked List

Lesson - 6

The Ultimate Guide To Understand The Differences Between Stack And Queue

Lesson - 7

Implementing Stacks in Data Structures

Lesson - 8

Your One-Stop Solution for Stack Implementation Using Array

Lesson - 9

Your One-Stop Solution for Queue Implementation Using Array

Lesson - 10

Your One-Stop Solution to Learn Depth-First Search(DFS) Algorithm From Scratch

Lesson - 11

Your One-Stop Solution for Stack Implementation Using Linked-List

Lesson - 12

The Definitive Guide to Understand Stack vs Heap Memory Allocation

Lesson - 13

All You Need to Know About Linear Search Algorithm

Lesson - 14

All You Need to Know About Breadth-First Search Algorithm

Lesson - 15

A One-Stop Solution for Using Binary Search Trees in Data Structure

Lesson - 16

The Best Tutorial to Understand Trees in Data Structure

Lesson - 17

A Complete Guide to Implement Binary Tree in Data Structure

Lesson - 18

A Holistic Look at Using AVL Trees in Data Structures

Lesson - 19

All You Need to Know About Tree Traversal in Data Structure

Lesson - 20

The Best Guide You’ll Ever Need to Understand B-Tree in Data Structure

Lesson - 21

The Best Guide You'll Ever Need to Understand Spanning Tree in Data Structure

Lesson - 22

The Best and Easiest Way to Understand an Algorithm

Lesson - 23

Your One-Stop Solution to Understand Shell Sort Algorithm

Lesson - 24

Your One-Stop Solution to Quick Sort Algorithm

Lesson - 25

The Most Useful Guide to Learn Selection Sort Algorithm

Lesson - 26

Everything You Need to Know About Radix Sort Algorithm

Lesson - 27

Everything You Need to Know About the Counting Sort Algorithm

Lesson - 28

Everything You Need to Know About the Merge Sort Algorithm

Lesson - 29

Insertion Sort Algorithm: One-Stop Solution That Will Help You Understand Insertion Sort

Lesson - 30

Everything You Need to Know About the Bubble Sort Algorithm

Lesson - 31

The Best Guide You’ll Ever Need to Understand Bucket Sort Algorithm

Lesson - 32

Your One-Stop Solution to Understand Recursive Algorithm in Programming

Lesson - 33

The Definitive Guide to Understanding Greedy Algorithm

Lesson - 34

Your One-Stop Solution to Understand Backtracking Algorithm

Lesson - 35

The Fundamentals of the Bellman-Ford Algorithm

Lesson - 36

Your One-Stop Solution for Graphs in Data Structures

Lesson - 37

The Best Guide to Understand and Implement Solutions for Tower of Hanoi Puzzle

Lesson - 38

A Simplified and Complete Guide to Learn Space and Time Complexity

Lesson - 39

All You Need to Know About the Knapsack Problem : Your Complete Guide

Lesson - 40

The Fibonacci Series: Mathematical and Programming Interpretation

Lesson - 41

The Holistic Look at Longest Common Subsequence Problem

Lesson - 42

The Best Article to Understand What Is Dynamic Programming

Lesson - 43

A Guide to Implement Longest Increasing Subsequence Using Dynamic Programming

Lesson - 44

A Holistic Guide to Learn Stop Solution Using Dynamic Programming

Lesson - 45
Your One-Stop Solution for Stack Implementation Using Array

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.

stack-implementation-using-array

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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.

push-operation-on-stack.

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.

pop-operation.

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.

peek-operation.

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];

}

}

Add Another Star to Your Performance Evaluation

Learn from industry experts for FREEStart Learning
Add Another Star to Your Performance Evaluation

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.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

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!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.