Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <vector>
- class Stack
- {
- private:
- struct StackNode
- {
- StackNode* nextNode;
- int value;
- };
- StackNode* topNode;
- public:
- Stack();
- Stack(const Stack& original);
- ~Stack();
- bool isEmpty() const;
- int top() const;
- int pop();
- void push(int);
- };
- Stack::Stack() : topNode(nullptr){}
- Stack::Stack(const Stack& original)
- {
- StackNode* oldCurrent = original.topNode;
- StackNode* current = nullptr;
- if(oldCurrent == nullptr)
- {
- topNode = nullptr;
- return;
- }
- else
- {
- current = new StackNode();
- current -> nextNode = nullptr;
- current -> value = oldCurrent -> value;
- topNode = current;
- oldCurrent = oldCurrent -> nextNode;
- }
- while(oldCurrent != nullptr)
- {
- StackNode* newNextNode = new StackNode();
- newNextNode -> nextNode = nullptr;
- newNextNode -> value = oldCurrent -> value;
- current -> nextNode = newNextNode;
- current = current -> nextNode;
- oldCurrent = oldCurrent -> nextNode;
- }
- }
- Stack::~Stack()
- {
- StackNode* currentNode = topNode;
- while(currentNode != nullptr)
- {
- StackNode* temp = currentNode->nextNode;
- delete currentNode;
- currentNode = temp;
- }
- }
- bool Stack::isEmpty() const
- {
- return topNode == nullptr;
- }
- int Stack::top() const
- {
- if(isEmpty())
- {
- throw std::runtime_error("stack is empty");
- }
- else
- {
- return topNode->value;
- }
- }
- int Stack::pop()
- {
- if(isEmpty())
- {
- throw std::runtime_error("stack is empty");
- }
- else
- {
- int temp = topNode->value;
- StackNode *oldRoot = topNode;
- topNode = oldRoot->nextNode;
- oldRoot->nextNode = nullptr;
- delete oldRoot;
- return temp;
- }
- }
- void Stack::push(int value)
- {
- StackNode* newNode = new StackNode();
- newNode->nextNode = topNode;
- newNode->value = value;
- topNode = newNode;
- }
- int numChecker(char num)
- {
- switch(num)
- {
- case '0' : return 0;
- break;
- case '1' : return 1;
- break;
- case '2' : return 2;
- break;
- case '3' : return 3;
- break;
- case '4' : return 4;
- break;
- case '5' : return 5;
- break;
- case '6' : return 6;
- break;
- case '7' : return 7;
- break;
- case '8' : return 8;
- break;
- case '9' : return 9;
- break;
- default : return -1;
- break;
- }
- }
- int main()
- {
- Stack s1 = Stack();
- std::string input;
- std::cout << "stack>\n";
- do
- {
- getline(std::cin, input);
- if(input.substr(0, 4).compare("push") == 0)
- {
- if(input.length() > 5)
- {
- try {
- int num = std::stol(input.substr(5, input.length()));
- s1.push(num);
- } catch (std::invalid_argument) {
- std::cout << "error: not a number\n";
- }
- }
- else
- {
- std::cout << "error: not a number\n";
- }
- }
- else if(input.substr(0, 3).compare("pop") == 0)
- {
- if(!s1.isEmpty())
- {
- std::cout << s1.pop() << "\n";
- }
- else
- {
- std::cout << "error: stack is empty\n";
- }
- }
- else if(input.compare("list") == 0)
- {
- Stack copy(s1);
- std::cout << "[";
- while(!copy.isEmpty())
- {
- std::cout << copy.pop();
- if (!copy.isEmpty())
- std::cout << ",";
- }
- std::cout << "]\n";
- }
- else if (input.compare("top") == 0)
- {
- if (!s1.isEmpty())
- {
- std::cout << s1.top() << "\n";
- }
- else
- {
- std::cout << "error: stack is empty\n";
- }
- }
- else if(input.compare("end") == 0 || std::cin.eof())
- {
- break;
- }
- else
- {
- std::cout << "error: invalid command\n";
- }
- std::cout << "stack> ";
- }
- while (1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement