Basic Operations on a Stack in C++
1. Push operation
This operation can be used to adds the element to the top of the stack.
Algorithm
1. Check if stack is full. If it is indicate the overflow condition.
2. Increment the top index.
3. Place the new element at top position in the array.
2. Pop Operation
This operation can be used to removes and returns the top element of the stack.
Algorithm
1. Check if stack is empty. If it is indicates the underflow condition.
2. Retrieve the element at top index.
3. Decrement the top index.
4. Return the retrieved element.
3. Peek Operation
The operation can be used to returns the top element without the removing it from the stack.
Algorithm
1. Check if stack is empty. If it is return an error or special value indicating the stack is empty.
2. Return the element at top index without the modifying the top.
4. IsEmpty operation
This operation can checks if the stack contains no elements of the stack.
Algorithm
1. Return the true if the top index is -1 then otherwise return false.
4. IsFull Operation
This operation can checks if the stack has been reached its the maximum capacity of the stack.
Algorithm
1. Return the true if top index is equal to maximum index otherwise return false.
C++ Program to Implement Stack using array
Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both understanding and coding.
Contact Us