Introduction to Stacks in Data Structures
Introduction to Stacks in Data Structures
Before we move on further with understanding the Stack Data Structure, we need to learn about the
basic terminology that is associated with this data structure so that understanding stack will be a little
easy for us.
Top – This refers to the topmost element of the stack, or in other words the element last
entered in the stack.
Push () – This is one of the operation that we can perform on stack, for inserting data in this
data structure.
Pop () – This operation deals with deleting the data from the stack. It deletes the top-most data
from the stack.
Peek () – This operation helps us in looking at the topmost element of the data without
removing it from the stack.
A stack may be implemented to have a bounded capacity. If the stack is full and does not contain
enough space for push operation, the stack is then considered to be in an overflow state.
Below is simple representation of a stack with push and pop operations.Limitation in using StackOne of
the major limitation in using stack is that it only operates on the topmost element of the stack.
Average Comparisons
(n+1)/2
2. Deck of cards
2. Backtracking
3. Memory management
4. Process Management
When a function is called the local variables are stored in a stack, and it is automatically
destroyed once returned.
Creating too many objects on the stack can increase the risk of stack overflow.
Variable storage will be overwritten, which sometimes leads to undefined behavior of the
function or program.
The stack will fall outside of the memory area, which might lead to an abnormal termination.
Operations on a Stack
A stack is a data structure in which the operations are executed in constant time.
There are a number of operations that can be performed on a stack, but there are two
major operations that are used to manipulate a stack.
Push()
Pop()
Push()
Let us understand how a push() operation works. Let us consider we have to push a
given number of elements in the stack.
Step 1:
Step 3:
Step 4:
Step 5:
Step 6:
Step 8:
Let us understand how a pop() operation works. Let us consider we have to pop a given
number of elements in the stack.
Step 1:
Step 3:
The next element to be deleted from the stack is 33.
We can see the value of top points to 33.
We perform the pop operation and the element 33 is popped out off the stack.
The remaining elements are shown in the image.
Top = 21.
class Stack
{
public:
int top;
int maxSize;
int* array;
Stack(int max)
{
top=-1;
maxSize=max;
array=new int[max];
}
int isFull()
{
if(top==maxSize-1)
cout<<"Will not be able to push maxSize reached"<<endl;
return top==maxSize-1;
}
int isEmpty()
{
if(top==-1)
cout<<"Will not be able to pop minSize reached"<<endl;
return top==-1;
}
void push(int item)
{
if(isFull()) return;
array[++top]=item;
cout<<"We have pushed "<<item<<" to stack"<<endl;
}
int pop()
{
if(isEmpty()) return INT_MIN;
return array[top--];
}
int peek()
{
if(isEmpty()) return INT_MIN;
return array[top];
}
};
int main()
{
Stack stack(10);
stack.push(5);
stack.push(10);
stack.push(15);
int flag=1;
while(flag)
{
if(!stack.isEmpty())
cout<<"We have popped "<< stack.pop()<<" from stack"<<endl;
else
cout<<"Can't Pop stack must be empty\n";
Output
#include<iostream>
using namespace std;
#define size 20
int Stack[size],top_of_stack;
int main()
{
int num=0;
int option=0;
top_of_stack=-1;
while(1)
{
cout<<"\n 1: Push \n 2: Pop \n 3: Display \n 4: Exit \n Enter one of your
choices: ";
cin>>option;
switch(option)
{
case 1:
cout<<"Enter the item you want to insert :";
cin>>num;
Push(Stack,num);
break;
case 2:
Pop(Stack);
break;
case 3:
display(Stack);
break;
case 4:
exit(0);
default:
cout<<"\nPlease enter a correct option\n";
break;
}
}
del_num=stack[top_of_stack];
top_of_stack--;
cout<<"Element deleted: "<<del_num<<endl;
return;
}
1: Push
2: Pop
3: Display
4: Exit
Enter one of your choices: 1
Enter the item you want to insert :1
1: Push
2: Pop
3: Display
4: Exit
1: Push
2: Pop
3: Display
4: Exit
Enter one of your choices: 2
Element deleted: 3
1: Push
2: Pop
3: Display
4: Exit
Enter one of your choices: 3
1: Push
2: Pop
3: Display
4: Exit
Enter one of your choices: 4
https://www.cs.usfca.edu/~galles/visualization/StackArray.html
https://www.youtube.com/watch?v=DKlXEAZYB7I&list=PLLOxZwkBK52Akgqf4oSWPOQO9ROWS
_9rc&index=14