Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. class Stack
  6. {
  7.     private:
  8.         struct StackNode
  9.         {
  10.             StackNode* nextNode;
  11.             int value;
  12.         };
  13.         StackNode*  topNode;
  14.     public:
  15.         Stack();
  16.         Stack(const Stack& original);
  17.         ~Stack();
  18.         bool isEmpty() const;
  19.         int top() const;
  20.         int pop();
  21.         void push(int);
  22. };
  23.  
  24. Stack::Stack() : topNode(nullptr){}
  25.  
  26. Stack::Stack(const Stack& original)
  27. {
  28.     StackNode* oldCurrent = original.topNode;
  29.     StackNode* current = nullptr;
  30.  
  31.     if(oldCurrent == nullptr)
  32.     {
  33.         topNode = nullptr;
  34.         return;
  35.     }
  36.     else
  37.     {
  38.         current = new StackNode();
  39.         current -> nextNode = nullptr;
  40.         current -> value = oldCurrent -> value;
  41.         topNode = current;
  42.  
  43.         oldCurrent = oldCurrent -> nextNode;
  44.     }
  45.  
  46.     while(oldCurrent != nullptr)
  47.     {
  48.         StackNode* newNextNode = new StackNode();
  49.         newNextNode -> nextNode = nullptr;
  50.         newNextNode -> value = oldCurrent -> value;
  51.  
  52.         current -> nextNode = newNextNode;
  53.  
  54.         current = current -> nextNode;
  55.         oldCurrent = oldCurrent -> nextNode;
  56.     }
  57. }
  58. Stack::~Stack()
  59. {
  60.     StackNode* currentNode = topNode;
  61.     while(currentNode != nullptr)
  62.     {
  63.         StackNode* temp = currentNode->nextNode;
  64.         delete currentNode;
  65.         currentNode = temp;
  66.     }
  67. }
  68.  
  69. bool Stack::isEmpty() const
  70. {
  71.     return topNode == nullptr;
  72. }
  73.  
  74. int Stack::top() const
  75. {
  76.     if(isEmpty())
  77.     {
  78.         throw std::runtime_error("stack is empty");
  79.     }
  80.     else
  81.     {
  82.         return topNode->value;
  83.     }
  84. }
  85.  
  86. int Stack::pop()
  87. {
  88.     if(isEmpty())
  89.     {
  90.         throw std::runtime_error("stack is empty");
  91.     }
  92.     else
  93.     {
  94.         int temp = topNode->value;
  95.         StackNode *oldRoot = topNode;
  96.         topNode = oldRoot->nextNode;
  97.         oldRoot->nextNode = nullptr;
  98.         delete oldRoot;
  99.  
  100.         return temp;
  101.     }
  102. }
  103.  
  104. void Stack::push(int value)
  105. {  
  106.     StackNode* newNode = new StackNode();
  107.     newNode->nextNode = topNode;
  108.     newNode->value = value;
  109.     topNode = newNode;
  110. }
  111.  
  112.  
  113.  
  114. int numChecker(char num)
  115. {
  116.     switch(num)
  117.     {
  118.         case '0' : return 0;
  119.             break;
  120.         case '1' : return 1;
  121.             break;
  122.         case '2' : return 2;
  123.             break;
  124.         case '3' : return 3;
  125.             break;
  126.         case '4' : return 4;
  127.             break;
  128.         case '5' : return 5;
  129.             break;
  130.         case '6' : return 6;
  131.             break;
  132.         case '7' : return 7;
  133.             break;
  134.         case '8' : return 8;
  135.             break;
  136.         case '9' : return 9;
  137.             break;
  138.         default : return -1;
  139.             break;
  140.     }
  141. }
  142.  
  143. int main()
  144. {
  145.     Stack s1 = Stack();
  146.  
  147.     std::string input;
  148.  
  149.     std::cout << "stack>\n";
  150.     do
  151.     {
  152.         getline(std::cin, input);
  153.         if(input.substr(0, 4).compare("push") == 0)
  154.         {
  155.             if(input.length() > 5)
  156.             {
  157.                 try {
  158.                    
  159.                     int num  = std::stol(input.substr(5, input.length()));  
  160.                     s1.push(num);
  161.                 } catch (std::invalid_argument) {
  162.                     std::cout << "error: not a number\n";
  163.                 }
  164.             }
  165.             else
  166.             {
  167.                 std::cout << "error: not a number\n";
  168.             }
  169.         }
  170.         else if(input.substr(0, 3).compare("pop") == 0)
  171.         {
  172.             if(!s1.isEmpty())
  173.             {
  174.                 std::cout << s1.pop() << "\n";
  175.             }
  176.             else
  177.             {
  178.                 std::cout << "error: stack is empty\n";
  179.             }
  180.         }
  181.         else if(input.compare("list") == 0)
  182.         {
  183.             Stack copy(s1);
  184.  
  185.             std::cout << "[";
  186.             while(!copy.isEmpty())
  187.             {
  188.                 std::cout << copy.pop();
  189.                 if (!copy.isEmpty())
  190.                     std::cout << ",";
  191.             }
  192.             std::cout << "]\n";
  193.         }
  194.         else if (input.compare("top") == 0)
  195.         {
  196.             if (!s1.isEmpty())
  197.             {
  198.                 std::cout << s1.top() << "\n";
  199.             }
  200.             else
  201.             {
  202.                 std::cout << "error: stack is empty\n";
  203.             }
  204.            
  205.         }
  206.         else if(input.compare("end") == 0 || std::cin.eof())
  207.         {
  208.             break;
  209.         }
  210.         else
  211.         {
  212.             std::cout << "error: invalid command\n";
  213.         }
  214.  
  215.         std::cout << "stack> ";
  216.     }
  217.     while (1);
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement