What is a Stack?
A stack is a linear data structure where elements are stored in the LIFO (Last In First Out)
principle where the last element inserted would be the first element to be deleted. A stack is an
Abstract Data Type (ADT), that is popularly used in most programming languages. It is named
stack because it has the similar operations as the real-world stacks, for example − a pack of
cards or a pile of plates, etc.
Stack Representation
A stack allows all data operations at one end only. At any given time, we can only access the top
element of a stack.
The following diagram depicts a stack and its operations
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can
either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to
implement stack using arrays, which makes it a fixed size stack implementation.
Types of Stack:
Fixed Size Stack :
As the name suggests, a fixed size stack has a fixed size and cannot grow or shrink dynamically.
If the stack is full and an attempt is made to add an element to it, an overflow error occurs. If
the stack is empty and an attempt is made to remove an element from it, an underflow error
occurs.
Dynamic Size Stack :
A dynamic size stack can grow or shrink dynamically. When the stack is full, it automatically
increases its size to accommodate the new element, and when the stack is empty, it decreases
its size. This type of stack is implemented using a linked list, as it allows for easy resizing of the
stack.
Basic Operations on Stack:
In order to make manipulations in a stack, there are certain operations provided to us.
1. push() to insert an element into the stack
2. pop() to remove an element from the stack
3. top() Returns the top element of the stack.
4. isEmpty() returns true if stack is empty else false.
5. isFull() returns true if the stack is full else false.
To implement stack, we need to maintain reference to the top item.
1. Push Operation on Stack
Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.
Algorithm for Push Operation:
Before pushing the element to the stack, we check if the stack is full .
If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the element to
the stack.
Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is inserted at
top position .
The elements can be pushed into the stack till we reach the capacity of the stack.
Stack Insertion: push()
#include <iostream>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Check if the stack is full*/
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
/* Function to insert into the stack */
int push(int data){
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
Cout<<"Could not insert data, Stack is full."<<endl;
}
return data;
}
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
cout<<"Stack Elements:"<<endl;
// print stack data
for(i = 0; i < 8; i++) {
cout<<stack[i];
}
return 0;
}
2. Pop Operation in Stack
Removes an item from the stack. The items are popped in the reversed order in which they are
pushed. If the stack is empty, then it is said to be an Underflow condition.
Algorithm for Pop Operation:
Before popping the element from the stack, we check if the stack is empty .
If the stack is empty (top == -1), then Stack Underflows and we cannot remove any
element from the stack.
Otherwise, we store the value at top, decrement the value of top by 1 (top = top – 1)
and return the stored top value.
Stack Deletion: pop()
/* Check if the stack is empty */
int isempty(){
if(top == -1)
return 1;
else
return 0;
}
/* Function to delete from the stack */
int pop(){
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
Cout<<"Could not retrieve data, Stack is empty."<<endl;
}
}
3. Retrieving topmost Element from Stack: 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 for Top Operation:
Before returning the top element from the stack, we check if the stack is empty.
If the stack is empty (top == -1), we simply print “Stack is empty”.
Otherwise, we return the element stored at index = top .
4. isEmpty Operation in Stack Data Structure:
Returns true if the stack is empty, else false.
Algorithm for isEmpty Operation:
Check for the value of top in stack.
If (top == -1), then the stack is empty so return true .
Otherwise, the stack is not empty so return false
5. isFull Operation in Stack Data Structure:
Returns true if the stack is full, else false.
Algorithm for isFull Operation:
Check for the value of top in stack.
If (top == capacity-1), then the stack is full so return true.
Otherwise, the stack is not full so return false.