Stack using Templates
Write a C++ Menu Driven Program for Stack using Templates. Here’s a Simple C++ Menu Driven Program for Stack using Templates in C++ Programming Language.
What are Templates in C++ ?
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..
Function Template :
The general form of a template function definition is shown here:
template <class type> ret-type func-name(parameter list)
{
// body of function
}
Class Template :
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:
template <class type> class class-name
{
.
.
.
}
Below is the source code for C++ Menu Driven Program for Stack using Templates which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
/* C++ Menu Driven Program for Stack using Templates */ #include<iostream> using namespace std; template<class T> class Stack { public: Stack(int MaxStackSize); ~Stack(){delete[] S;} int IsEmpty()const{return top==-1;} int IsFull()const{return top==MaxTop;} T Peek()const; void Push(T); T Pop(); void Display(); private: int top; //current top of stack int MaxTop; //max val for top T *S; //element array }; template<class T> Stack<T>::Stack(int MaxStackSize) { //stack constructor MaxTop=MaxStackSize-1; S=new T[MaxStackSize]; top=-1; } template<class T> T Stack<T>::Peek()const { if(IsEmpty()) //top fails return 0; else return S[top]; } template<class T> void Stack<T>::Push(T x) { if(IsFull()) cout<<"\nno memory()"; //add fails else { S[++top]=x; } } template<class T> T Stack<T>::Pop() { T x; if(IsEmpty()) { cout<<"\nstack is empty\n"; return -1; } else { x=S[top--]; return x; } } template<class T> void Stack<T>::Display() { if(IsEmpty()) cout<<"\nout of bounds"; //delete fails else for(int i=top;i>=0;i--) { cout<<S[i]<<"\t"; } } void menu() { cout<<"\n1.Push\n 2.Pop\n 3.Peek\n 4.Display\n"; } int main() { Stack<int>iobj(5); int ch,x; do { menu(); cout<<"\nenter the choice\n"; cin>>ch; switch(ch) { case 1: cout<<"\nenter x value to push into the stack\n"; cin>>x; iobj.Push(x); break; case 2: x=iobj.Pop(); if(x!=-1) cout<<"\npoped value is \t"<<x<<endl; break; case 3: x=iobj.Peek(); cout<<"\ntop most value is \t"<<x<<endl; break; case 4: iobj.Display(); break; } }while(ch>=1&&ch<=4); return 0; } |
OUTPUT : :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
/* C++ Menu Driven Program for Stack using Templates */ 1.Push 2.Pop 3.Peek 4.Display enter the choice 1 enter x value to push into the stack 1 1.Push 2.Pop 3.Peek 4.Display enter the choice 1 enter x value to push into the stack 2 1.Push 2.Pop 3.Peek 4.Display enter the choice 1 enter x value to push into the stack 3 1.Push 2.Pop 3.Peek 4.Display enter the choice 1 enter x value to push into the stack 4 1.Push 2.Pop 3.Peek 4.Display enter the choice 1 enter x value to push into the stack 5 1.Push 2.Pop 3.Peek 4.Display enter the choice 3 top most value is 5 1.Push 2.Pop 3.Peek 4.Display enter the choice 4 5 4 3 2 1 1.Push 2.Pop 3.Peek 4.Display enter the choice 2 poped value is 5 1.Push 2.Pop 3.Peek 4.Display enter the choice 2 poped value is 4 1.Push 2.Pop 3.Peek 4.Display enter the choice 2 poped value is 3 1.Push 2.Pop 3.Peek 4.Display enter the choice 2 poped value is 2 1.Push 2.Pop 3.Peek 4.Display enter the choice 2 poped value is 1 1.Push 2.Pop 3.Peek 4.Display enter the choice 2 stack is empty 1.Push 2.Pop 3.Peek 4.Display enter the choice 4 out of bounds 1.Push 2.Pop 3.Peek 4.Display enter the choice 5 Process returned 0 |
Above is the source code and output for C++ Menu Driven Program for Stack using Templates which is successfully compiled and run on Windows System to produce desired output.
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.
Thanks for reading the post….