Today we are going to explain the operations of Stack in C++ using Array. Operations of Stacks like Push and pop operation Also the Limitations of Arrays and its benefits.
Stack in C++
A stack in C++ is a fundamental data structure in computer science that follows the Last In, First Out (LIFO) principle. It operates much like a stack of plates or books where you can only add or remove items from the top.
In a stack, elements are added and removed from the same end, known as the “top” of the stack. This means that the most recently added element is the first one to be removed.
Operations of stack in C++
The key operations of a stack are:
- Push Operation
- pop operation
- Top Element
- Empty(or IsEmpty)
push operation
The push operation is used to add an element onto the top of the stack. When a new element is pushed onto the stack, it becomes the new top element, and the size of the stack increases by one. This operation is also known as insertion.
In terms of implementation, when using an array, pushing an element involves appending it to the end of the array or incrementing a pointer to the top of the stack and then placing the element at that position.
void push(int x)
{
//if top is on last index
if (top == n - 1) {
cout << "stack is over flow" << endl;
return;
}
top++;
arr[top] = x;
}
pop operation
The pop operation is used to remove the top element from the stack. After popping an element, the size of the stack decreases by one, and the element that was removed is returned or discarded, depending on the implementation. This operation is also known as deletion. In array-based implementations, popping involves removing the element at the top of the stack by decrementing the top pointer or index.
void pop()
{
if (top == NULL) {
cout << "no element to pop" << endl;
return;
}
top--;
}
Top (or Peek)
The top operation, also known as peek, is used to retrieve the top element of the stack without removing it. It allows you to access the element that is currently at the top of the stack without modifying the stack’s contents. This operation is useful for inspecting the top element or performing operations based on its value.
int topElement()
{
if (top == NULL) {
cout << "no element in stack" << endl;
return -1;
}
else
return arr[top];
}
Empty (or IsEmpty)
The empty operation, sometimes called isEmpty, is used to check if the stack is empty or not. It returns a boolean value indicating whether the stack contains any elements or if it is devoid of elements. This operation is crucial for avoiding errors like attempting to pop from an empty stack.
bool empty()
{
if (top == NULL) {
return 1;
}
else
return 0;
}
Benefits of Array-Based Stack Implementation
Implementing a stack using arrays offers several advantages:
- Simplicity: The implementation is straightforward, making it easy to understand and maintain.
- Efficiency: Both push and pop operations have a time complexity of O(1), providing efficient performance for basic stack operations.
- Memory Efficiency: Arrays allocate contiguous memory, ensuring memory efficiency compared to other data structures that might require dynamic memory allocation.
- Flexibility: Arrays can be resized dynamically, allowing the stack to grow or shrink as needed.
Limitations
While array-based stack implementations offer many advantages, it’s essential to consider potential limitations, such as:
- Fixed Size: Traditional arrays have a fixed size, which can limit the capacity of the stack. However, this can be mitigated by using dynamic arrays or by implementing resizing mechanisms.
- Wasted Space: If the stack’s capacity is greater than the number of elements it contains, there may be wasted space in the array. Again, dynamic resizing can help optimize memory usage.
NOTE
Get the Project Files from GitHub.
You can also download its Zip File by clicking on Download button.
Conclusion
Implementing an operations of Stack in C++ using Array provides a simple yet powerful solution for managing data in a Last In, First Out fashion. With efficient push and pop operations, along with the flexibility and memory efficiency of arrays, this approach offers an ideal balance of performance and simplicity.
Whether you’re tackling algorithmic problems, building software applications, or exploring computer science concepts, understanding and mastering stack implementation using arrays is a valuable skill that will serve you well in your programming journey. So, embrace the power of arrays and unlock the full potential of stack-based solutions in your projects.
FAQ
What is a stack in computer science?
A stack is a data structure that follows the Last In, First Out (LIFO) principle. It allows elements to be added or removed only from the top.
What is the difference between a stack and a queue?
A stack follows the LIFO (Last In, First Out) principle, while a queue follows the FIFO (First In, First Out) principle. In a stack, elements are added and removed from the same end (the top), while in a queue, elements are added at the rear and removed from the front.
How can I implement a stack?
Stacks can be implemented using different underlying data structures, such as arrays, linked lists, or dynamic arrays. Each implementation has its own advantages and trade-offs in terms of efficiency and memory usage.
Code of operations of Stack in C++ using Array
This is the complete code of stack and its operations in C++ using Array.
// stackBasic.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
#define n 100
class stack
{
int* arr;
int top;
public:
stack()
{
arr = new int[n];
top = NULL;
}
void push(int x)
{
//if top is on last index
if (top == n - 1) {
cout << "stack is over flow" << endl;
return;
}
top++;
arr[top] = x;
}
void pop()
{
if (top == NULL) {
cout << "no element to pop" << endl;
return;
}
top--;
}
int topElement()
{
if (top == NULL) {
cout << "no element in stack" << endl;
return -1;
}
else
return arr[top];
}
bool empty()
{
if (top == NULL) {
return 1;
}
else
return 0;
}
};
int main()
{
stack stack;
//cout << "Pushing elements onto stack" << endl;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
cout << "current top element after pushes: " << endl;
cout << stack.topElement() << endl;
cout << "popping top element: " << endl;
stack.pop();
cout << stack.topElement() << endl;
cout << "popping top element: " << endl;
stack.pop();
cout << stack.topElement() << endl;
cout << "popping top element: " << endl;
stack.pop();
cout << stack.topElement() << endl;
stack.pop();
cout << "stack is now empty." << endl;
cout << stack.empty() << endl;
// return;
}