Skip to content

Instantly share code, notes, and snippets.

@skshetry
Last active June 17, 2017 15:49
Show Gist options
  • Save skshetry/5770ef362bd20828ec3d9ac116b33306 to your computer and use it in GitHub Desktop.
Save skshetry/5770ef362bd20828ec3d9ac116b33306 to your computer and use it in GitHub Desktop.
This was meant "To be API compatible" stack implementation with C++ STL stack that couldn't be and uses vector under the hood.
#include "stack.hpp"
////////////////////////////////// This is an implementation file ///////////////////////////////////////////////////////
///////////////////////////////// How to use this in your code? ///////////////////////////////////////////////////////
/*
First include "stack.hpp> in your source file at the top.
1. g++ -o <outputbinaryfilename> <yourfilename> stack.cpp
2. g++ -o stack.o -c stack.cpp
g++ -o main.o -c <yourfilename>
g++ -o main.o stack.o
*/
template<typename T> void Stack<T>::push(T incomingdata){
stackdata.push_back(incomingdata);
}
template<typename T> void Stack<T>::pop(){
if(!empty()) stackdata.pop_back();
}
template<typename T> T Stack<T>::top(){
return stackdata.back();
}
template<typename T>int Stack<T>::empty(){
return stackdata.empty();
}
template<typename T> int Stack<T>::size(){
return stackdata.size();
}
//// stack is an API compatible version of Stack-STL
#include <vector>
#ifndef STACK_HPP
#define STACK_HPP
#endif
template <typename T>
class Stack{
std::vector<T> stackdata;
public:
/// Programming Interfaces
void push(T incomingdata);
T top();
void pop();
int empty();
void swap();
int size();
};
// Explicit template instantiation
template class Stack<int>;
template class Stack<char>;
template class Stack<double>;
template class Stack<float>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment