C++ Program to Implement Stack using array
Last Updated :
24 May, 2024
Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both understanding and coding.
Implementation of Stack in C++
The stack can be implemented using the array organizes its the elements in contiguous memory locations. We can enclose this array in a class as a member and encapsulate the methods and data related to the stack in that class for easy and organized access.
class Stack {
public:
int stackArr[size];
int stackSize;
// constructors and methods
};
Basic Operations on a Stack in C++
1. Push operation
This operation can be used to adds the element to the top of the stack.
Algorithm
1. Check if stack is full. If it is indicate the overflow condition.
2. Increment the top index.
3. Place the new element at top position in the array.
2. Pop Operation
This operation can be used to removes and returns the top element of the stack.
Algorithm
1. Check if stack is empty. If it is indicates the underflow condition.
2. Retrieve the element at top index.
3. Decrement the top index.
4. Return the retrieved element.
3. Peek Operation
The operation can be used to returns the top element without the removing it from the stack.
Algorithm
1. Check if stack is empty. If it is return an error or special value indicating the stack is empty.
2. Return the element at top index without the modifying the top.
4. IsEmpty operation
This operation can checks if the stack contains no elements of the stack.
Algorithm
1. Return the true if the top index is -1 then otherwise return false.
4. IsFull Operation
This operation can checks if the stack has been reached its the maximum capacity of the stack.
Algorithm
1. Return the true if top index is equal to maximum index otherwise return false.
C++ Program to Implement Stack using array
C++
// Include necessary libraries
#include <iostream>
using namespace std;
// Define Stack class
class Stack {
// Pointer to an array that stores elements of the stack
int* arr;
// Index of the top element in the stack
int top;
// Maximum size of the stack
int capacity;
public:
// Constructor to initialize the stack
Stack(int size)
{
// Allocate memory for the stack
arr = new int[size];
// Set the maximum size of the stack
capacity = size;
// Initialize the top of the stack as -1 indicating
// the stack is empty
top = -1;
}
// Destructor to deallocate memory
~Stack() { delete[] arr; }
// Function to add an element x in the stack
void push(int x)
{
// Check if the stack is full
if (isFull()) {
cout << "Overflow\n";
return;
}
cout << "Pushing " << x << "\n";
// Add element and increment top
arr[++top] = x;
}
// Function to remove an element from the stack
int pop()
{
// Check if the stack is empty
if (isEmpty()) {
cout << "Underflow\n";
return -1;
}
// Remove element and decrement top
return arr[top--];
}
// Function to return the top element of the stack
int peek()
{
if (!isEmpty())
return arr[top];
else
return -1;
}
// Function to return if the stack is empty
bool isEmpty() { return top == -1; }
// Function to return if the stack is full
bool isFull() { return top == capacity - 1; }
};
// Main function
int main()
{
// Create a stack of size 3
Stack stack(3);
// Push elements into the stack
stack.push(10);
stack.push(20);
stack.push(30);
// Print the top element of the stack
cout << "The top element is " << stack.peek() << endl;
// Pop an element from the stack and print it
cout << "Popping " << stack.pop() << endl;
// Print the top element of the stack
cout << "The top element is " << stack.peek() << endl;
// Pop all elements from the stack
stack.pop();
stack.pop();
// Check if the stack is empty and print the result
if (stack.isEmpty()) {
cout << "The stack is empty" << endl;
}
else {
cout << "The stack is not empty" << endl;
}
return 0;
}
OutputPushing 10
Pushing 20
Pushing 30
The top element is 30
Popping 30
The top element is 20
The stack is empty
Time and Space Complexity
- Time complexity: O(1)
- Space complexity: O(n), where n is number of elements in array
Applications of the Stack
- It can applies the Function Call Management.
- It can be applies on the Expression Evaluation to convert the infix expressions to postfix.
- It can be used in the Backtracking algorithms.
- It can be applies on the complier use stacks for the syntax checking and parsing.
- It can applies on the Undo Mechanisms like text editors.
Similar Reads
C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read
Implement Stack using Array Stack is a linear data structure which follows LIFO principle. 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 ap
10 min read
Implementation of Stack Using Array in C A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means that the most recently added element is the first one to be removed. In this article, we will learn how to implement a stack using an array in C. Implementation of Stack Using Arrays in CIn the array-b
5 min read
Implement a Stack in C Programming Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate
7 min read
JavaScript program to implement queue using stack A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to
3 min read
JavaScript program to implement stack using queue In this article, we implement a JavaScript program to make a stack using a queue data structure. It provides essential stack methods like push(), pop(), and peek(), isEmpty() operations, utilizing either one or two queues to simulate the behavior of a stack. Examples: Input:push(2)push(3)pop()peek()
4 min read
How to implement a Stack using list in C++ STL In this article, we will discuss how to implement a Stack using list in C++ STL. Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:1. Push: Push an element into the stack.2. Pop: Removes the element by following
3 min read
Java Program to Implement Stack Data Structure Stack is the fundamental Data Structure that can follow the Last In, First Out(LIFO) principle. It can work that the last element added to the stack will be the first one to be removed. It can operate like a stack of plates: We can only add or remove the topmost plate at any given time. The simplici
5 min read
Implementing Stack Using Class Templates in C++ The task is to implement some important functions of stack like pop(), push(), display(), topElement(), isEmpty(), isFull() using class template in C++. Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or
5 min read
Perl | Implementing a Stack Stack in Perl is a linear data structure that follows the LIFO (Last In First Out) or FILO (First In Last Out) order. In simpler terms, a stack is an array in which insertion and deletion takes place at only one end called the top of the stack. Pushing is the process of insertion of elements into a
4 min read