DS Lab-1
DS Lab-1
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.
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:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.
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
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.
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
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 .
https://www.geeksforgeeks.org/implement-stack-using-array/ 3/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks
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
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.
https://www.geeksforgeeks.org/implement-stack-using-array/ 6/7
1/24/25, 10:43 PM Implement Stack using Array - GeeksforGeeks
https://www.geeksforgeeks.org/implement-stack-using-array/ 7/7