3.0 Stack
3.0 Stack
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas
the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost
element of the stack. Whenever an element is added in the stack, it is added on the top of the stack, and the
element can be deleted only from the stack. In other words, a stack can be defined as a container in which
insertion and deletion can be done from the one end known as the top of the stack.
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the
stack; therefore, the size of the stack is 5.
Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the
stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full.
When we perform the delete operation on the stack, there is only one way for entry and exit as the other end
is closed. It follows the LIFO pattern, which means that the value entered first will be removed last. In the
above case, the value 5 is entered first, so it will be removed only after the deletion of al l the other elements.
Standard Stack Operations
The following are some common operations implemented on the stack:
o push(): When we insert an element in a stack then the operation is known as a push. If the stack is full
then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is
empty means that no element exists in the stack, this state is known as an underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
PUSH operation
The steps involved in the PUSH operation is given below: (Algorithm)
o Before inserting an element in a stack, we check whether the stack is full.
o If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
o When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
o When the new element is pushed in a stack, first, the value of the top gets incremented,
i.e., top=top+1, and the element will be placed at the new position of the top.
o The elements will be inserted until we reach the max size of the stack.
#include <iostream>
#include <stack>
int main() {
// Create an empty stack of integers
std::stack<int> myStack;
return 0;
}
POP operation
The steps involved in the POP operation is given below:
o Before deleting the element from the stack, we check whether the stack is empty.
o If we try to delete the element from the empty stack, then the underflow condition occurs.
o If the stack is not empty, we first access the element which is pointed by the top
o Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
Algorithm :
1. begin
2. if top = 0 then stack empty;
3. item := stack(top);
4. top = top - 1;
5. end;
#include <iostream>
#include <stack>
int main() {
// Create an empty stack of integers
std::stack<int> myStack;
return 0;
}
peek()
The peek() is an operation retrieves the topmost element within the stack, without deleting it. This operation
is used to check the status of the stack with the help of the top pointer.
Algorithm of pee()
1. START
2. return the element at the top of the stack
3. END
#include <iostream>
#include <stack>
int main() {
// Create a stack of integers
std::stack<int> myStack;
bool isFull() {
return top == MAX_SIZE - 1; // Check if the stack is full
}
int pop() {
if (top >= 0) {
return stack[top--]; // Return the top element and decrement 'top'
} else {
std::cout << "Stack is empty. Cannot pop." << std::endl;
return -1; // Return a sentinel value for an empty stack
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
push(60); // This will trigger the "Stack is full" message
return 0;
}
isEmpty()
The isEmpty() operation verifies whether the stack is empty. This operation is
used to check the status of the stack with the help of top pointer.
Algorithm
1. START
2. If the top value is -1, the stack is empty. Return 1.
3. Otherwise, return 0.
4. END
#include <iostream>
#include <stack>
int main() {
std::stack<int> myStack;
std::cout << "Is the stack empty? " << (isEmpty(myStack) ? "Yes" : "No") << std::endl;
myStack.push(10);
myStack.push(20);
myStack.push(30);
std::cout << "Is the stack empty? " << (isEmpty(myStack) ? "Yes" : "No") << std::endl;
return 0;
}