Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int type;
  6.     Node *prev;
  7. };
  8. struct Stack {
  9.     int size = 0;
  10.     Node *top = nullptr;
  11. };
  12. void pushNode(Stack *stPtr, int newType);
  13. void extractNode(Stack *stPtr);
  14. void clear(Stack *stPtr);
  15. int commonSize = 0, maximum = 0;
  16. int main() {
  17.     int requestNumber, stackAmount, maxAmount;
  18.     cin >> requestNumber >> stackAmount >> maxAmount;
  19.     Stack **array = new Stack *[stackAmount];
  20.     for (int i = 0; i < stackAmount; ++i) {
  21.         array[i] = new Stack;
  22.     }
  23.     bool status = true;
  24.     for (int i = 0; i < requestNumber && status; ++i) {
  25.         char command;
  26.         int newType, stackIndex;
  27.         cin >> command >> stackIndex >> newType;
  28.         if (command == '+') {
  29.             pushNode(array[stackIndex - 1], newType);
  30.         }
  31.         else if (command == '-') {
  32.             if (array[stackIndex - 1]->size == 0 || array[stackIndex - 1]->top->type != newType) {
  33.                 status = false;
  34.             }
  35.             else {
  36.                 extractNode(array[stackIndex - 1]);
  37.             }
  38.         }
  39.     }
  40.     if (status && commonSize == 0) {
  41.         cout << maximum << endl;
  42.     }
  43.     else {
  44.         cout << "Error" << endl;
  45.     }
  46.     for (int i = 0; i < stackAmount; ++i) {
  47.         clear(array[i]);
  48.     }
  49.     delete []array;
  50.     return 0;
  51. }
  52. void pushNode(Stack *stPtr, int newType) {
  53.     Node *newNode = new Node {newType, stPtr->top};
  54.     stPtr->top = newNode;
  55.     ++stPtr->size;
  56.     ++commonSize;
  57.     maximum = (maximum < commonSize) ? commonSize : maximum;
  58. }
  59. void extractNode(Stack *stPtr) {
  60.     Node *oldTop = stPtr->top;
  61.     stPtr->top = oldTop->prev;
  62.     delete oldTop;
  63.     --stPtr->size;
  64.     --commonSize;
  65. }
  66. void clear(Stack *stPtr) {
  67.     while(stPtr->size != 0) {
  68.         extractNode(stPtr);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement