Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "stack.h"
- #include "rover_util.h"
- StackNode::~StackNode() {
- delete next;
- }
- stack::stack() {
- head = nullptr;
- }
- stack::~stack(){
- //Stack does not delete it's contents as this stack does not manage their lifetimes
- delete head;
- }
- stack::stack(const stack &other) {
- copyStack(other);
- }
- stack& stack::operator=(stack other) {
- copyStack(other);
- return *this;
- }
- void stack::copyStack(const stack other) {
- StackNode *currentOther, *currentThis, *tmp;
- delete head;
- if(other.head == nullptr) {
- head = nullptr;
- } else {
- head = new StackNode;
- head->data = other.head->data;
- currentThis = head;
- currentOther = other.head->next;
- while(currentOther != nullptr) {
- tmp = new StackNode;
- tmp->data = currentOther->data;
- currentThis->next = tmp;
- currentThis = tmp;
- currentOther = currentOther->next;
- }
- }
- }
- void stack::push(RoverOperation* data){
- StackNode *tmp = new StackNode;
- tmp->next = head;
- tmp->data = data;
- head = tmp;
- count = count + 1;
- }
- RoverOperation* stack::pop(){
- RoverOperation *result;
- StackNode *tmp;
- if(count == 0) {
- result = nullptr;
- } else {
- result = head->data;
- std::cout << "Hmmmmm" << std::endl;
- std::cout << result->x;
- tmp = head;
- head = head->next;
- delete tmp;
- count = count - 1;
- }
- return result;
- }
- RoverOperation* stack::peek()const {
- RoverOperation* result;
- if(count == 0) {
- result = nullptr;
- } else {
- result = head->data;
- }
- return result;
- }
- bool stack::isEmpty() const {
- return count == 0;
- }
Add Comment
Please, Sign In to add comment