Skip to content

Instantly share code, notes, and snippets.

@ahmedalkabir
Created November 10, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedalkabir/d635398fcdeb7da284d885d9b2518e21 to your computer and use it in GitHub Desktop.
Save ahmedalkabir/d635398fcdeb7da284d885d9b2518e21 to your computer and use it in GitHub Desktop.
STACK Implementation
#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