Introduction to Stack
Have you seen a bunch of books in library kept one over the another ? Of course everyone has seen .If I want to take a book from that bunch located at top of bunch I would simply pick it up and read it .Similarly if I want to put one book in that bunch ,I have to put it on the top of it .But think about the situation where I want to take a book located at middle of the bunch ,how should I take it ? Think ....
Yes.... I have to remove all the books above that book and take it to read.
Stack of Books |
In the exact same way the stack data structure is implemented.
What is Stack Data Structure ?
Stack is a linear data structure used for temporary storage of data .The insertion and deletion of data elements take place only at one end of the stack called 'Top'. Hence Top is only the entry and exit point in stack . Another examples of stack include stack of dishes , coins ,plates.
- The element which is inserted first will be deleted last .
- The element which is inserted last will be deleted first .
Insertion of data element is called as 'Push' while deletion is called as 'Pop'.
Stack as Abstract Data Type (ADT)
An abstraction of data is an abstraction of data structure which may be defined as a collection of data values and functions which operates on these data values . ADT is concerned only with data or properties which are affected .
Stack as ADT is defined with help of following :
Data Items : A stack contain ' data ' and ' Top ' as data items .
Operations : A Stack is an ADT with these operations
- IsEmpty() : This function is used to check whether stack is empty or not
- IsFull() : This function is used to check whether stack is full or not .
- Push() : This function is used to insert the element at the top of stack.
- Pop() : This function deletes the top element from the stack .
- display() : This function is used to display all the elements of stack.
Stack Implementation Using Array
As stack is a collection of homogeneous data elements , array can be easily used to implement the stack data structure .
Declare an array of named stack of size Max where Max is the maximum number of elements that array named stack can hold .
Stack using array |
Initially when stack is empty , Top = -1 .
One end of stack is kept fixed and other end is kept open for insertion and deletion of data elements .
Stack grows or shrink as elements are pushed or poped at the Top end .
Now , let's discuss the operations on stack in detail .
IsEmpty() :
This function returns true if the stack is empty , else it returns false . The condition for stack empty is Top = -1 .
When stack is completely empty , this condition is called as 'Underflow condition'.
Function definition :
bool IsEmpty()
{
if ( Top == -1 )
{
return true;
}
else
{
return false;
}
}
{
if ( Top == -1 )
{
return true;
}
else
{
return false;
}
}
IsFull() :
This function returns true if stack is full . The stack full condition is if Top = Max -1 .
When stack is completely filled , this condition is called as 'Overflow condition '.
When stack is completely filled , this condition is called as 'Overflow condition '.
bool IsFull()
{
if(Top == Max - 1)
{
return true;
}
else
{
return false;
}
{
if(Top == Max - 1)
{
return true;
}
else
{
return false;
}
Push(int value) :
This function is used to insert the element into the stack . The prerequisite for this function is that stack should not be full .
It takes one parameter of type int or float or char depending upon the data type of stack array .
To insert the data at the top of stack , we have to increment the Top by 1 and put the value at Top index .
It takes one parameter of type int or float or char depending upon the data type of stack array .
To insert the data at the top of stack , we have to increment the Top by 1 and put the value at Top index .
void push(int value)
{
if ( IsFull() )
{
cout<<"Stack Overflow !";
cout<<"endl";
}
else
{
Top = Top + 1;
stack[ Top] = value;
}
}
{
if ( IsFull() )
{
cout<<"Stack Overflow !";
cout<<"endl";
}
else
{
Top = Top + 1;
stack[ Top] = value;
}
}
Consider an example , an array of size n named stack . We have push elements in it . So initially when stack is empty , Top = -1 or NULL .
To push 10 , Top is incremented by 1 and value is placed at index Top .
The graphical representation of push operation is shown in diagram .
Push Operation in Stack |
20 and 30 are push in same manner.
Pop() :
Pop() :
This function is used to delete an element from the stack . The prerequisite for using pop function is that stack should not be empty .
This function do not take any parameter.
It simply returns the deleted value to main function .
int pop( )
{
int x;
x = stack[Top];
Top = Top - 1;
return x;
}
{
int x;
x = stack[Top];
Top = Top - 1;
return x;
}
Consider an example , with stack of elements four . When we call the pop function , Top element is deleted and Top variable is decremented by 1 . Now Top points to the 30 . The poped element (40) is returned to main function .
Similarly when again pop() function is called , it deletes 30 and returns to main . Top now points to 20 .
In the same way 20 and 10 are poped .
C++ Code for Implementation of Stack using Array
C++ Code for Implementation of Stack using Array
#include<iostream>
using namespace std;
bool IsEmpty();
bool IsFull();
void push(int value);
int pop();
void display();
int Top;
#define Max 5
int stack[Max];
int main()
{
int data;
Top = -1 ;
int option;
cout<<"\n\n\t \t\t Operations on Stack \n\n";
do
{
cout<<"-----------------------------------------------------------------------------------------------------\n";
cout<<"\t\t 1.Push \n";
cout<<"\t\t 2.Pop \n";
cout<<"\t\t 3.Display \n";
cout<<"\t\t 4.Check whether Stack is Empty \n";
cout<<"\t\t 5.Check whether Stack is Full \n";
cout<<"\t\t 6.Exit \n";
cout<<"Enter your choise : ";
cin>>option;
switch(option)
{
case 1:
if( IsFull( ) )
cout<<"\n\t \t\t Sorry !! Stack Overflow ! \n\n";
else
{
cout<<"Enter the data : ";
cin>>data;
push(data);
}
break;
case 2:
if( IsEmpty() )
cout<<"\n\t \t\t Sorry !! Stack Underflow ! \n\n";
else
{
cout<<"\n\t \t\t Element popped is : "<<pop()<<"\n\n";
}
break;
case 3:
cout<<"\nDisplaying the content of Stack in reverse order : \n";
display();
break;
case 4:
if(IsEmpty())
cout<<"\n\n\t \t\t Stack is Empty ! \n\n";
else
cout<<"\n\t \t\t Stack is not Empty \n\n";
break;
case 5:
if(IsFull())
cout<<"\t \t\t Stack is Full ! \n";
else
cout<<"\n\t \t\t Stack is not Full \n";
break;
}
}while(option<=5);
}
bool IsEmpty()
{
if ( Top == -1 )
{
return true;
}
else
{
return false;
}
}
bool IsFull()
{
if(Top == Max - 1)
{
return true;
}
else
{
return false;
}
}
void push(int value)
{
if( IsFull() )
{
cout<<"Stack Overflow !";
cout<<endl;
}
else
{
Top = Top + 1;
stack[ Top] = value;
}
}
int pop()
{
int x;
x = stack[Top];
Top = Top - 1;
return x;
}
void display()
{
int temp;
for(temp =Top;temp>=0;temp--)
{
cout<<"\t "<<stack[temp]<<endl;
}
}
using namespace std;
bool IsEmpty();
bool IsFull();
void push(int value);
int pop();
void display();
int Top;
#define Max 5
int stack[Max];
int main()
{
int data;
Top = -1 ;
int option;
cout<<"\n\n\t \t\t Operations on Stack \n\n";
do
{
cout<<"-----------------------------------------------------------------------------------------------------\n";
cout<<"\t\t 1.Push \n";
cout<<"\t\t 2.Pop \n";
cout<<"\t\t 3.Display \n";
cout<<"\t\t 4.Check whether Stack is Empty \n";
cout<<"\t\t 5.Check whether Stack is Full \n";
cout<<"\t\t 6.Exit \n";
cout<<"Enter your choise : ";
cin>>option;
switch(option)
{
case 1:
if( IsFull( ) )
cout<<"\n\t \t\t Sorry !! Stack Overflow ! \n\n";
else
{
cout<<"Enter the data : ";
cin>>data;
push(data);
}
break;
case 2:
if( IsEmpty() )
cout<<"\n\t \t\t Sorry !! Stack Underflow ! \n\n";
else
{
cout<<"\n\t \t\t Element popped is : "<<pop()<<"\n\n";
}
break;
case 3:
cout<<"\nDisplaying the content of Stack in reverse order : \n";
display();
break;
case 4:
if(IsEmpty())
cout<<"\n\n\t \t\t Stack is Empty ! \n\n";
else
cout<<"\n\t \t\t Stack is not Empty \n\n";
break;
case 5:
if(IsFull())
cout<<"\t \t\t Stack is Full ! \n";
else
cout<<"\n\t \t\t Stack is not Full \n";
break;
}
}while(option<=5);
}
bool IsEmpty()
{
if ( Top == -1 )
{
return true;
}
else
{
return false;
}
}
bool IsFull()
{
if(Top == Max - 1)
{
return true;
}
else
{
return false;
}
}
void push(int value)
{
if( IsFull() )
{
cout<<"Stack Overflow !";
cout<<endl;
}
else
{
Top = Top + 1;
stack[ Top] = value;
}
}
int pop()
{
int x;
x = stack[Top];
Top = Top - 1;
return x;
}
void display()
{
int temp;
for(temp =Top;temp>=0;temp--)
{
cout<<"\t "<<stack[temp]<<endl;
}
}
Note : This code is successfully compiled and run under Linux distribution Ubuntu 18.04 LTS environment .
Output :
Operations on Stack
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 1
Enter the data : 10
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 1
Enter the data : 20
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 1
Enter the data : 30
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 3
Displaying the content of Stack in reverse order :
30
20
10
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 4
Stack is not Empty
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 5
Stack is not Full
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 2
Element popped is : 30
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 2
Element popped is : 20
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 2
Element popped is : 10
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 4
Stack is Empty !
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 1
Enter the data : 10
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 1
Enter the data : 20
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 1
Enter the data : 30
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 3
Displaying the content of Stack in reverse order :
30
20
10
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 4
Stack is not Empty
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 5
Stack is not Full
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 2
Element popped is : 30
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 2
Element popped is : 20
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 2
Element popped is : 10
-----------------------------------------------------------------------------------------------------
1.Push
2.Pop
3.Display
4.Check whether Stack is Empty
5.Check whether Stack is Full
6.Exit
Enter your choise : 4
Stack is Empty !
-----------------------------------------------------------------------------------------------------
Time Complexity :
Push and Pop operation : O(1) .
Conclusion :
Stack , a linear data structure can be easily implemented using array .
0 Comments