Created
November 10, 2018 22:23
-
-
Save ahmedalkabir/d635398fcdeb7da284d885d9b2518e21 to your computer and use it in GitHub Desktop.
STACK Implementation
This file contains 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<vector> | |
using namespace std; | |
template<typename T, int num> | |
class Stack{ | |
public: | |
void push(const T& t){ | |
if(top >= num){ | |
cout << "STACK IS FULL" << endl; | |
}else{ | |
top++; | |
elements.push_back(t); | |
} | |
} | |
T pop(){ | |
if(top <= -1){ | |
cout << "STACK is empty"<< endl; | |
return 0; | |
}else{ | |
auto x = elements.back(); | |
elements.pop_back(); | |
top--; | |
return x; | |
} | |
} | |
void show(){ | |
if( top >= 0){ | |
cout << "******* ELEMENTS *******\n" << endl; | |
for(int i=top; i >= 0; i--){ | |
cout << "The Element : " << elements[i] << endl; | |
} | |
}else{ | |
cout << "STACK is empty"<< endl; | |
} | |
} | |
private: | |
vector<T> elements; | |
int top = -1; | |
}; | |
int main(void){ | |
Stack<int, 5> stack_1; | |
int choice, temp; | |
cout << " Stack Implementation" << endl; | |
cout << " 1.push\n 2.pop\n 3.show\n 4.exit" << endl; | |
do{ | |
cout << "Enter YOUR Choice :"; | |
cin >> choice; | |
switch(choice){ | |
case 1: | |
cout << "\n Enter Number to push to the stack :"; | |
cin >> temp; | |
stack_1.push(temp); | |
break; | |
case 2: | |
cout << "The popped element is " << stack_1.pop() << endl;; | |
break; | |
case 3: | |
stack_1.show(); | |
break; | |
case 4: | |
cout << "\n EXITING....." << endl; | |
break; | |
default: | |
cout << "\n\t Invaild option " << endl; | |
break; | |
} | |
}while(choice != 4); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment