Stack (Array Implementation), Code β Language
Harsh Mishra
Posted on August 4, 2024
Creation of Stack (Array Implementation)
class Stack {
private:
int* arr;
int top;
int capacity;
public:
// Constructor to initialize stack
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
};
First, we will create a class named
Stack
.-
It will contain three properties:
-
int* arr
: Points to the array that will store stack elements. -
int top
: Stores the index of the top element in the stack. -
int capacity
: Stores the maximum number of elements the stack can hold.
-
-
We will declare a constructor
Stack(int size)
:- It will take
size
as input. - We will dynamically allocate memory for the stack array with the given size and assign it to
arr
. - We will set the
capacity
to the given size. - We will set
top
to-1
, indicating an empty stack.
- It will take
Push Operation
void push(int item) {
if (isFull()) {
cout << "Overflow: Stack is full.\n";
return;
}
arr[++top] = item;
}
We will define a method push(int item)
:
- It will take an integer
item
as input, which is the value to be pushed onto the stack. - We will check if the stack is full (using
isFull()
); if so, it will print "Overflow: Stack is full." and return. - Otherwise, it will increment the
top
index and assign the valueitem
to the stack at the updatedtop
index.
Pop Operation
int pop() {
if (isEmpty()) {
cout << "Underflow: Stack is empty.\n";
return -1;
}
return arr[top--];
}
We will define a method pop()
:
- It will return an integer, which is the value removed from the top of the stack.
- We will check if the stack is empty (using
isEmpty()
); if so, it will print "Underflow: Stack is empty." and return-1
. - Otherwise, it will return the value at the
top
index and then decrement thetop
index.
Peek Operation
int peek() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return -1;
}
return arr[top];
}
We will define a method peek()
:
- It will return an integer, which is the value at the top of the stack.
- We will check if the stack is empty (using
isEmpty()
); if so, it will print "Stack is empty." and return-1
. - Otherwise, it will return the value at the
top
index.
isEmpty Operation
bool isEmpty() {
return top == -1;
}
We will define a method isEmpty()
:
- It will return a boolean value indicating whether the stack is empty.
- It will return
true
iftop
is-1
, indicating that the stack is empty. - Otherwise, it will return
false
.
isFull Operation
bool isFull() {
return top == capacity - 1;
}
We will define a method isFull()
:
- It will return a boolean value indicating whether the stack is full.
- It will return
true
iftop
is equal tocapacity - 1
, indicating that the stack has reached its maximum capacity. - Otherwise, it will return
false
.
Size Operation
int size() {
return top + 1;
}
We will define a method size()
:
- It will return an integer representing the number of elements currently in the stack.
- It will return
top + 1
, which gives the count of elements in the stack.
Full Code Implementation
#include <iostream>
using namespace std;
class Stack {
private:
int* arr;
int top;
int capacity;
public:
// Constructor to initialize stack
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
// Destructor to free allocated memory
~Stack() {
delete[] arr;
}
// Push operation
void push(int item) {
if (isFull()) {
cout << "Overflow: Stack is full.\n";
return;
}
arr[++top] = item;
}
// Pop operation
int pop() {
if (isEmpty()) {
cout << "Underflow: Stack is empty.\n";
return -1;
}
return arr[top--];
}
// Peek operation
int peek() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return -1;
}
return arr[top];
}
// Check if stack is empty
bool isEmpty() {
return top == -1;
}
// Check if stack is full
bool isFull() {
return top == capacity - 1;
}
// Get the size of the stack
int size() {
return top + 1;
}
};
π πͺ π
π©
Harsh Mishra
Posted on August 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
algorithms Mastering Algorithms: Essential Concepts Every Computer Engineering Student Should Know
October 31, 2024
java The Ultimate Guide to Graphs in Java: A Deep Dive for Developers of All Levels
November 11, 2024
javascript Cracking Stack and Queue LeetCode Problems: A Step-by-Step Guide to Solving LeetCode Challenges
October 10, 2024