Full Code
Stack Class Explanation
🚀 Stack Class Overview
- Uses a template to support any data type.
- Maximum size is set to
MAX_SIZE = 100
.
- Contains an array
items
to store elements and an integer top
to track the index of the top element.
- Implements a LIFO (Last In, First Out) stack.
🛠Constructor: Stack()
Stack()
{
top = -1;
}
- Initializes the
top
to -1
indicating the stack is empty when created.
🛠bool isFull()
bool isFull()
{
return top == MAX_SIZE - 1;
}
- Returns
true
if the stack has reached its maximum capacity.
- Means no more elements can be pushed.
🛠void push(T element)
void push(T element)
{
if (isFull())
{
cout << "Stack is full. Cannot push " << element << endl;
return;
}
top++;
items[top] = element;
}
- Adds a new element on top of the stack.