Last active
September 28, 2018 04:02
-
-
Save theArjun/98b5ee33550d63ef3491ba000431b6a8 to your computer and use it in GitHub Desktop.
Class Template for Stack of Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<conio.h> | |
| using namespace std; | |
| template<class X> | |
| class Stack{ | |
| private: | |
| X index; | |
| X pile[100]; | |
| public: | |
| /*void initialization(){ | |
| index=0; | |
| }*/ | |
| /*This function can be put into Constructor so that it initialize itself for the new objects. So, I am putting in it Constructor.*/ | |
| Stack():index(0){} /*Default constructor to set the index 0.*/ | |
| void push(X temp){ | |
| if(index==100){ | |
| cout << "\nStack is full.\n"; | |
| return; | |
| } | |
| pile[index++]=temp; | |
| } | |
| X pop(){ | |
| if(index==0){ | |
| cout << "\nStack is empty.\n"; | |
| } | |
| return pile[--index]; | |
| } | |
| }; | |
| int main(){ | |
| Stack<int> s1; | |
| /*s1.pop(); You will get the message "Stack is empty." here*/ | |
| // s1.initialization(); | |
| s1.push(100); | |
| s1.push(20); | |
| s1.push(30); | |
| cout << s1.pop() << endl; | |
| cout << s1.pop() << endl; | |
| cout << s1.pop() << endl; | |
| getch(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment