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
sizeas input. - We will dynamically allocate memory for the stack array with the given size and assign it to
arr. - We will set the
capacityto the given size. - We will set
topto-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
itemas 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
topindex and assign the valueitemto the stack at the updatedtopindex.
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
topindex and then decrement thetopindex.
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
topindex.
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
trueiftopis-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
trueiftopis 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;
}
};
Top comments (0)