#include #include #include #include "DoublyLinkedList.hpp" // Ex1 template void insertSorted(std::list& ll, const T& elem) { auto iter = ll.cbegin(); while (*iter < elem) ++iter; ll.insert(iter, elem); } void ex1() { std::list l; l.push_back(1); l.push_back(2); l.push_back(4); l.push_back(5); for (int n : l) std::cout << n << " "; std::cout << std::endl; insertSorted(l, 3); for (int n : l) std::cout << n << " "; std::cout << std::endl; } // Ex2 void print(DoublyLinkedList*>& dll) { for (std::queue* val : dll) std::cout << val->front() << " "; std::cout << std::endl; } int operate(int a, int b, char op) { switch(op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } int evaluateRPQueue(std::queue& eq) { int accum = eq.front() - '0'; eq.pop(); char buf; while (!eq.empty()) { buf = eq.front(); eq.pop(); accum = operate(accum, buf - '0', eq.front()); eq.pop(); } return accum; } void evaluateQueues(DoublyLinkedList*>& dll) { for (std::queue* eq : dll) { eq->push(evaluateRPQueue(*eq)); } } template bool isSymmetricBad(DoublyLinkedList*> dll) { while (!dll.empty()) { if (dll.back()->front() != dll.front()->front()) return false; dll.popBack(); if (dll.empty()) return true; dll.popFront(); } return true; } template bool isSymmetric(DoublyLinkedList*>& dll) { auto frontIter = dll.begin(); auto backIter = --dll.end(); while (frontIter != backIter) { if ((*frontIter)->front() != (*backIter)->front()) return false; ++frontIter; if (frontIter == backIter) return true; --backIter; } return true; } void ex2() { DoublyLinkedList*> dll; dll.pushBack(new std::queue()); dll.back()->push('4'); dll.back()->push('5'); dll.back()->push('+'); dll.pushBack(new std::queue()); dll.back()->push('1'); dll.back()->push('6'); dll.back()->push('+'); dll.back()->push('7'); dll.back()->push('*'); dll.pushBack(new std::queue()); dll.back()->push('8'); dll.pushBack(new std::queue()); dll.back()->push('1'); dll.back()->push('7'); dll.back()->push('*'); dll.back()->push('7'); dll.back()->push('*'); dll.pushBack(new std::queue()); dll.back()->push('2'); dll.back()->push('3'); dll.back()->push('*'); dll.back()->push('3'); dll.back()->push('+'); print(dll); evaluateQueues(dll); print(dll); std::cout << isSymmetric(dll) << std::endl; } int main() { ex2(); }