Last active
June 17, 2017 15:49
-
-
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.
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 "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(); | |
} |
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
//// 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