Guest User

Untitled

a guest
Jul 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. #include "stack.h"
  5. #include "rover_util.h"
  6.  
  7. StackNode::~StackNode() {
  8. delete next;
  9. }
  10.  
  11. stack::stack() {
  12. head = nullptr;
  13. }
  14.  
  15. stack::~stack(){
  16. //Stack does not delete it's contents as this stack does not manage their lifetimes
  17. delete head;
  18. }
  19.  
  20. stack::stack(const stack &other) {
  21. copyStack(other);
  22. }
  23.  
  24. stack& stack::operator=(stack other) {
  25. copyStack(other);
  26. return *this;
  27. }
  28.  
  29.  
  30. void stack::copyStack(const stack other) {
  31. StackNode *currentOther, *currentThis, *tmp;
  32.  
  33. delete head;
  34.  
  35. if(other.head == nullptr) {
  36. head = nullptr;
  37. } else {
  38. head = new StackNode;
  39. head->data = other.head->data;
  40. currentThis = head;
  41. currentOther = other.head->next;
  42.  
  43. while(currentOther != nullptr) {
  44. tmp = new StackNode;
  45. tmp->data = currentOther->data;
  46. currentThis->next = tmp;
  47. currentThis = tmp;
  48. currentOther = currentOther->next;
  49. }
  50. }
  51. }
  52.  
  53.  
  54. void stack::push(RoverOperation* data){
  55. StackNode *tmp = new StackNode;
  56. tmp->next = head;
  57. tmp->data = data;
  58. head = tmp;
  59. count = count + 1;
  60. }
  61.  
  62. RoverOperation* stack::pop(){
  63. RoverOperation *result;
  64. StackNode *tmp;
  65. if(count == 0) {
  66. result = nullptr;
  67. } else {
  68. result = head->data;
  69. std::cout << "Hmmmmm" << std::endl;
  70. std::cout << result->x;
  71. tmp = head;
  72. head = head->next;
  73. delete tmp;
  74. count = count - 1;
  75. }
  76. return result;
  77. }
  78.  
  79. RoverOperation* stack::peek()const {
  80. RoverOperation* result;
  81. if(count == 0) {
  82. result = nullptr;
  83. } else {
  84. result = head->data;
  85. }
  86. return result;
  87. }
  88.  
  89. bool stack::isEmpty() const {
  90. return count == 0;
  91. }
Add Comment
Please, Sign In to add comment