Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. //
  2. // operationStack.h
  3. // hw2
  4. //
  5. // Created by Dolendra Subedi on 3/15/18.
  6. // Copyright © 2018 Paul. All rights reserved.
  7. //
  8.  
  9. #ifndef myStack_h
  10. #define myStack_h
  11. #include "Node.h"
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. template <class T>
  16.  
  17. class myStack{
  18. public:
  19. Node<T>* top;
  20.  
  21. myStack();
  22. bool isEmpty();
  23. Node<T>* pop();
  24. T peek();
  25. void push(T);
  26. };
  27.  
  28.  
  29. template <class T>
  30. myStack<T>::myStack(){
  31. top=NULL;
  32.  
  33. }
  34.  
  35. template <class T>
  36. bool myStack<T>:: isEmpty(){
  37.  
  38. if (top==NULL) {
  39. return true;
  40. }
  41.  
  42. return false;
  43. }
  44.  
  45.  
  46. template <class T>
  47. Node<T>* myStack<T>:: pop(){
  48.  
  49. if (top==NULL) {
  50. cout<<"empty"<<endl;
  51. return NULL;
  52. }
  53.  
  54. Node<T>* temp = top;
  55. top=temp->down;
  56. return temp;
  57.  
  58. }
  59.  
  60. template <class T>
  61. T myStack<T>::peek(){
  62. if (top==NULL) {
  63. return NULL;
  64. }
  65. return top->val;
  66.  
  67. }
  68.  
  69.  
  70. template <class T>
  71. void myStack<T>::push(T s){
  72.  
  73. Node<T>* newNode=new Node<T>(s);
  74.  
  75. if (top==NULL) {
  76. top= newNode;
  77. return;
  78. }
  79.  
  80. Node<T>* temp=top;
  81.  
  82. newNode->down=temp;
  83. top=newNode;
  84.  
  85.  
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. #endif /* myStack_h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement