0% found this document useful (0 votes)
18 views7 pages

DS Lab-1

The document explains how to implement a stack data structure using arrays, following the LIFO principle. It details the operations of push, pop, peek, isEmpty, and isFull, along with their respective algorithms and C++ code examples. Additionally, it discusses the time complexity and the advantages and disadvantages of using an array for stack implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

DS Lab-1

The document explains how to implement a stack data structure using arrays, following the LIFO principle. It details the operations of push, pop, peek, isEmpty, and isFull, along with their respective algorithms and C++ code examples. Additionally, it discusses the time complexity and the advantages and disadvantages of using an array for stack implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1/24/25, 10:42 PM Implement Stack using Array - GeeksforGeeks

Implement Stack using Array


Last Updated : 15 Apr, 2024

Stack is a linear data structure which follows LIFO principle. In this article, we will learn how
to implement Stack using Arrays. In Array-based approach, all stack-related operations are
executed using arrays. Let’s see how we can implement each operation on the stack utilizing the
Array Data Structure.

Implement Stack using Array:

To implement a stack using an array, initialize an array and treat its end as the stack’s top.
Implement push (add to end), pop (remove from end), and peek (check end) operations,
handling cases for an empty or full stack.

Step-by-step approach:

1. Initialize an array to represent the stack.


2. Use the end of the array to represent the top of the stack.
3. Implement push (add to end), pop (remove from the end), and peek (check end) operations,
ensuring to handle empty and full stack conditions.

Implement Stack Operations using Array:


Here are the following operations of implement stack using array:

Push Operation in Stack:

Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.

Algorithm for Push Operation:

Before pushing the element to the stack, we check if the stack is full .
If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the
element to the stack.

https://www.geeksforgeeks.org/implement-stack-using-array/ 1/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks

Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is
inserted at top position .
The elements can be pushed into the stack till we reach the capacity of the stack.

1/6

Pop Operation in Stack:

Removes an item from the stack. The items are popped in the reversed order in which they are
pushed. If the stack is empty, then it is said to be an Underflow condition.

Algorithm for Pop Operation:

Before popping the element from the stack, we check if the stack is empty .
If the stack is empty (top == -1), then Stack Underflows and we cannot remove any
element from the stack.
Otherwise, we store the value at top, decrement the value of top by 1 (top = top –
1) and return the stored top value.

https://www.geeksforgeeks.org/implement-stack-using-array/ 2/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks

1/6

Top or Peek Operation in Stack:

Returns the top element of the stack.

Algorithm for Top Operation:

Before returning the top element from the stack, we check if the stack is empty.
If the stack is empty (top == -1), we simply print “Stack is empty”.
Otherwise, we return the element stored at index = top .

isEmpty Operation in Stack:

Returns true if the stack is empty, else false.

Algorithm for isEmpty Operation :

Check for the value of top in stack.


If (top == -1) , then the stack is empty so return true .
Otherwise, the stack is not empty so return false .

isFull Operation in Stack :

Returns true if the stack is full, else false.

Algorithm for isFull Operation:

https://www.geeksforgeeks.org/implement-stack-using-array/ 3/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks

Check for the value of top in stack.


If (top == capacity-1), then the stack is full so return true .
Otherwise, the stack is not full so return false.

Below is the implementation of the above approach:

C++ C Java Python3 C# JavaScript

1
/* C++ program to implement basic stack

2
operations */

3
#include <bits/stdc++.h>

5
using namespace std;

7
#define MAX 1000

9
class Stack {

10
int top;

11

12
public:

https://www.geeksforgeeks.org/implement-stack-using-array/ 4/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks

13
int a[MAX]; // Maximum size of Stack

14

15
Stack() { top = -1; }

16
bool push(int x);

17
int pop();

18
int peek();

19
bool isEmpty();

20
};

21

22
bool Stack::push(int x)

23
{

24
if (top >= (MAX - 1)) {

25
cout << "Stack Overflow";

26
return false;

27
}

28
else {

https://www.geeksforgeeks.org/implement-stack-using-array/ 5/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks

29
a[++top] = x;

30
cout << x << " pushed into stack\n";

31
return true;

32
}

33
}

Output

10 pushed into stack


20 pushed into stack
30 pushed into stack
30 Popped from stack
Top element is : 20
Elements present in stack : 20 10

Complexity Analysis:

Time Complexity:
push: O(1)
pop: O(1)
peek: O(1)
is_empty: O(1)
is_full: O(1)
Auxiliary Space: O(n), where n is the number of items in the stack.

Advantages of Array Implementation:


Easy to implement.
Memory is saved as pointers are not involved.

https://www.geeksforgeeks.org/implement-stack-using-array/ 6/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks

Disadvantages of Array Implementation:


It is not dynamic i.e., it doesn’t grow and shrink depending on needs at runtime. [But in case
of dynamic sized arrays like vector in C++, list in Python, ArrayList in Java, stacks can grow
and shrink with array implementation as well].
The total size of the stack must be defined beforehand.

https://www.geeksforgeeks.org/implement-stack-using-array/ 7/7

You might also like