Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int type;
- Node *prev;
- };
- struct Stack {
- int size = 0;
- Node *top = nullptr;
- };
- void pushNode(Stack *stPtr, int newType);
- void extractNode(Stack *stPtr);
- void clear(Stack *stPtr);
- int commonSize = 0, maximum = 0;
- int main() {
- int requestNumber, stackAmount, maxAmount;
- cin >> requestNumber >> stackAmount >> maxAmount;
- Stack **array = new Stack *[stackAmount];
- for (int i = 0; i < stackAmount; ++i) {
- array[i] = new Stack;
- }
- bool status = true;
- for (int i = 0; i < requestNumber && status; ++i) {
- char command;
- int newType, stackIndex;
- cin >> command >> stackIndex >> newType;
- if (command == '+') {
- pushNode(array[stackIndex - 1], newType);
- }
- else if (command == '-') {
- if (array[stackIndex - 1]->size == 0 || array[stackIndex - 1]->top->type != newType) {
- status = false;
- }
- else {
- extractNode(array[stackIndex - 1]);
- }
- }
- }
- if (status && commonSize == 0) {
- cout << maximum << endl;
- }
- else {
- cout << "Error" << endl;
- }
- for (int i = 0; i < stackAmount; ++i) {
- clear(array[i]);
- }
- delete []array;
- return 0;
- }
- void pushNode(Stack *stPtr, int newType) {
- Node *newNode = new Node {newType, stPtr->top};
- stPtr->top = newNode;
- ++stPtr->size;
- ++commonSize;
- maximum = (maximum < commonSize) ? commonSize : maximum;
- }
- void extractNode(Stack *stPtr) {
- Node *oldTop = stPtr->top;
- stPtr->top = oldTop->prev;
- delete oldTop;
- --stPtr->size;
- --commonSize;
- }
- void clear(Stack *stPtr) {
- while(stPtr->size != 0) {
- extractNode(stPtr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement