SHARE
TWEET

Untitled

a guest Apr 19th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef MYSTACK_H_
  2. #define MYSTACK_H_
  3. /////////////////////////////////////////////////////////////////////////////
  4.  
  5. #include "LinkList.h" //single linked-list char only
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. template<class T>
  10. class MyStack {
  11. public:
  12.     MyStack();
  13.  
  14.     void push(T _data);       //O(1)
  15.     void pop();               //O(1)
  16.     T top();                  //O(1)
  17.     bool isEmpty();           //O(1)
  18.     void clear();             //O(n)
  19.     int size();               //O(1)
  20.     void print();
  21. private:
  22.     LinkList<T> Link;
  23.     int sz;
  24. };
  25.  
  26. template<class T>
  27. MyStack<T>::MyStack() {
  28.     sz = 0;
  29. }
  30.  
  31. template<class T>
  32. void MyStack<T>::push(T _data) {
  33.     Link.addfront(_data);
  34.     sz++;
  35. }
  36.  
  37. template<class T>
  38. void MyStack<T>::pop() {
  39.     Link.erasefront();
  40.     (sz == 0 ? sz = 0 : sz--);
  41. }
  42.  
  43. template<class T>
  44. T MyStack<T>::top() {
  45.     return Link.showfront();
  46. }
  47.  
  48. template<class T>
  49. bool MyStack<T>::isEmpty() {
  50.     return Link.isempty();
  51. }
  52.  
  53. template<class T>
  54. void MyStack<T>::clear() {
  55.     Link.clear();
  56. }
  57.  
  58. template<class T>
  59. int MyStack<T>::size() {
  60.     return sz;
  61. }
  62.  
  63. template<class T>
  64. void MyStack<T>::print() {
  65.     Link.print();
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. #endif /* MYSTACK_H_ */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top