The task is to implement some important functions of stack like pop(), push(), display(), topElement(), isEmpty(), isFull() using class template in C++. Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second keyword can always be replaced by the keyword ‘class’.
Illustration:
Consider an example of plates stacked over one another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
Example:
C++14
#include <iostream>
#include <string>
using namespace std;
#define SIZE 5
template < class T> class Stack {
public :
Stack();
void push(T k);
T pop();
T topElement();
bool isFull();
bool isEmpty();
private :
int top;
T st[SIZE];
};
template < class T> Stack<T>::Stack() { top = -1; }
template < class T> void Stack<T>::push(T k)
{
if (isFull()) {
cout << "Stack is full\n" ;
}
cout << "Inserted element " << k << endl;
top = top + 1;
st[top] = k;
}
template < class T> bool Stack<T>::isEmpty()
{
if (top == -1)
return 1;
else
return 0;
}
template < class T> bool Stack<T>::isFull()
{
if (top == (SIZE - 1))
return 1;
else
return 0;
}
template < class T> T Stack<T>::pop()
{
T popped_element = st[top];
top--;
return popped_element;
}
template < class T> T Stack<T>::topElement()
{
T top_element = st[top];
return top_element;
}
int main()
{
Stack< int > integer_stack;
Stack<string> string_stack;
integer_stack.push(2);
integer_stack.push(54);
integer_stack.push(255);
string_stack.push( "Welcome" );
string_stack.push( "to" );
string_stack.push( "GeeksforGeeks" );
cout << integer_stack.pop() << " is removed from stack"
<< endl;
cout << string_stack.pop() << " is removed from stack "
<< endl;
cout << "Top element is " << integer_stack.topElement()
<< endl;
cout << "Top element is " << string_stack.topElement()
<< endl;
return 0;
}
|
OutputInserted element 2
Inserted element 54
Inserted element 255
Inserted element Welcome
Inserted element to
Inserted element GeeksforGeeks
255 is removed from stack
GeeksforGeeks is removed from stack
Top element is 54
Top element is to
Output explanation:
Here two data types (integer and string) are implemented using a single stack class. First, two objects are taken one is for integer class and the second is for the string class, Elements are inserted in both the classes using push() and isFull() method of stack class. Elements are removed using pop and isEmpty() functions of stack class. Finally, the top element is printed for each class using the top element() function.