void test_qa(AnsType& expectAns, OpreateType& opreateParam, InitType& initData, DataType1 firstData = DataType1()) { AnsType ans; MyStack work; for(int i=0; i<opreateParam.size(); i++) { int ansTmp = -1; if(opreateParam[i] == "push") { work.push(firstData[i]); } if(opreateParam[i] == "pop") { ansTmp = work.pop(); } if(opreateParam[i] == "top") { ansTmp = work.top(); } if(opreateParam[i] == "empty") { ansTmp = work.empty(); } ans.push_back(ansTmp); } int index = getIndex(); bool check = eq(ans, expectAns); if(!check) { printf("index %d: NO\n", index); output("opreateParam", opreateParam); output("initData", initData); output("firstData", firstData); output("ans", ans); output("expectAns", expectAns); } else { printf("index %d: YES\n", index); } printf("\n"); }
int main(){ MyStack<int> a; MyQueue<int> c; a.push(4); a.push(5); cout << a.top() << endl; a.pop(); cout << a.top() << endl; c.enqueue(4); c.enqueue(5); cout << c.front() << endl; c.dequeue(); cout << c.front() << endl; }
bool StackWithMin::push(int d){ Node* p = new Node(d); p->next = head; head = p; ++size; if(d<minValue){ minS.push(d); minS.top(minValue); } return true; }
void processChar( char item, MyStack<Token> &stack, string &operations, bool &OK ) { // Note: // I could check top of stack before adding tokens // and catch many errors "now" instead of later. // I'd rather be lazy and let collapse() catch the errors. Token token = toToken(item); switch (token) { case TOKEN_VALUE: if ( stack.isEmpty() ) { stack << token; return; } switch ( stack.top() ) { case TOKEN_LEFT_GROUP: stack << token; break; case TOKEN_ADDITION: stack << token; break; // don't collapse yet case TOKEN_MULTIPLICATION: collapse( stack << token, operations, OK ); break; default: OK = false; break; } break; case TOKEN_ADDITION: collapse( stack, operations, OK); stack << TOKEN_ADDITION; break; case TOKEN_MULTIPLICATION: case TOKEN_LEFT_GROUP: stack << token; break; case TOKEN_RIGHT_GROUP: // clear any pending addidion collapse( stack, operations, OK ); if ( !OK ) return; // convert ( value ) to value if ( !stack.isEmpty() && TOKEN_VALUE == stack.pop() && !stack.isEmpty() && TOKEN_LEFT_GROUP == stack.pop() ) stack << TOKEN_VALUE; else OK = false; break; case TOKEN_UNKNOWN: OK = false; break; } }
// remove one complete arithmetic operation, if it is there void collapse( MyStack<Token> &stack, string &operations, bool &OK ) { // It should end with a value if ( stack.isEmpty() || stack.top() != TOKEN_VALUE ) return; stack.pop(); // if that was the ONLY thing on the stack or it is preceeded by (, it is OK if ( stack.isEmpty() || TOKEN_LEFT_GROUP == stack.top() ) { stack << TOKEN_VALUE; return; } // The value should be preceeded with an operator Token operation; if ( stack.isEmpty() || !isOperator(operation = stack.pop()) ) { OK = false; return; } operations += ((operation==TOKEN_ADDITION) ? "+" : "*"); // The operator should be preceeded with a value if ( stack.isEmpty() || stack.pop() != TOKEN_VALUE ) { OK = false; return; } // sucessful collapse - to a single value stack << TOKEN_VALUE; }
bool StackWithMin::pop(int& d){ if(empty()) return false; d = head->data; Node* p = head; head = head->next; delete p; --size; if(d==minValue){ minS.pop(minValue); minS.top(minValue); if(minS.empty()) minValue = INT_MAX; } return true; }
int main() { MyStack<int> st; for (int i = 0; i < 100; i++) st.push(i); std::cout << st.size() << std::endl << std::endl; for (int i = 0; i < 10; i++) st.pop(); std::cout << st.size() << std::endl << std::endl; for (int i = 0; i < 10; i++) st.push(i); std::cout << st.size() << std::endl << std::endl; while (!st.empty()) { int k; st.top(k); std::cout << k << std::endl; st.pop(); } std::cout << st.size() << std::endl << std::endl; return 0; }
int libFunc() { MyStack s; return s.top(); }