int main(void) { MyStack<char> *pStack = new MyStack<char>(20); MyStack<char> *pNeedStack = new MyStack<char>(20); char str[] = "[[]>]]"; char isNeeded = 0; cout << strlen(str) << endl; for (int i = 0; i < strlen(str); i++) { cout << str[i] << endl; if (str[i] != isNeeded) { pStack->push(str[i]); cout << "push stack: " << str[i] << endl; switch(str[i]) { case '[': if (0 != isNeeded) { pNeedStack->push(isNeeded); cout << "push isNeeded: " << isNeeded << endl; } isNeeded = ']'; break; case '(': if (0 != isNeeded) { pNeedStack->push(isNeeded); cout << "push isNeeded: " << isNeeded << endl; } isNeeded = ')'; break; default: cout << "string is not matched." << endl; return 0; break; } } else { char temp = 0; pStack->pop(temp); cout << "pop stack: " << temp << endl; if (!pNeedStack->pop(isNeeded)) { isNeeded = 0; } cout << "pop isNeeded: " << isNeeded << endl; } } if (pStack->stackEmpty()) { cout << "string is matched" << endl; } else { cout << "string is not matched" << endl; } delete pStack; pStack = NULL; delete pNeedStack; pNeedStack = NULL; return 0; }
int main (void) { MyStack *pStack = new MyStack(5); pStack->push('a'); pStack->push('e'); pStack->push('i'); pStack->push('o'); pStack->push('u'); //pStack->clearStack(); pStack->stackTraverse(true); char elem = 0; pStack->pop(elem); cout << elem << endl; pStack->stackTraverse(true); cout << pStack -> stackLength() << endl; if(pStack->stackEmpty()){ cout << "栈为空" << endl; } if(pStack->stackFull()){ cout << "栈为满" << endl; } delete pStack; pStack = NULL; return 0; }
int main() { cout<<"----------MyStack<int>---------------"<<endl; MyStack<int> mysatck; mysatck.push(1); mysatck.push(2); mysatck.push(3); cout<<mysatck.pop()<<endl; cout<<mysatck.pop()<<endl; cout<<mysatck.pop()<<endl; cout<<"----------deque<int>---------------"<<endl; deque<int> dequeint; dequeint.push_front(1); dequeint.push_front(2); dequeint.push_front(3); cout<<dequeint.front()<<endl; dequeint.pop_front(); cout<<dequeint.front()<<endl; dequeint.pop_front(); cout<<dequeint.front()<<endl; dequeint.pop_front(); return 0; }
/* Function: sortTokenByStacks() * Usage: is called by formulaStringScanning() for single token; * -----------------------------------------------------------------------------------------// * Sort this token through the stacks. If token is number - push it to stackNumbers. * If token is valid operator token - process it due to Shunting-Yard conditions. * * @param token Current token in formula string * @param stackNumbers Stack of number values for current recursion * @param stackOperators Stack of operators for current recursion */ void sortTokenByStacks(string token, MyStack<double> &stackNumbers, MyStack<string> &stackOperators) { if(stringIsDouble(token)) { //Token is number double num = stringToDouble(token); stackNumbers.push(num);//Just save token to stack } else { // Token is operator /* Main operators process */ if(stackOperators.isEmpty()) { //Empty - push there without conditions stackOperators.push(token); } else { //If there are some operators in stack string topOper = stackOperators.peek();//Get top operator if(getOperPrecedence(topOper) < getOperPrecedence(token)) { /* Top operator precednce is * weaker then this token operator - just save this token */ stackOperators.push(token); } else { /* Top operator precednce is higher - evaluate two top numbers * with top operator, and sort current token again */ if(!failFlag) { //If there some fails - break this function /* Main calculation for top numbers and top operator */ twoNumsProcess(stackNumbers, stackOperators); /* Call sorting again to process current token operator */ sortTokenByStacks(token, stackNumbers, stackOperators); } } } } }
int pop() { while(!S1.isEmpty()) S2.push(S1.pop()); temp = S2.pop(); while(!S2.isEmpty()) S1.push(S2.pop()); return temp; }
bool solveMas(string s) { MyStack<char> st; char skob[3][2]; skob[0][0] = '('; skob[1][0] = '['; skob[2][0] = '{'; skob[0][1] = ')'; skob[1][1] = ']'; skob[2][1] = '}'; for (int i = 0 ; i < s.size() ; i++) { char c = s[i]; int curJ = -1, curK; for (int j = 0 ; j < 3 ; j++) { for (int k = 0 ; k < 2 ; k++) { if (skob[j][k] == c) { curJ = j, curK = k; break; } } if (curJ != -1) break; } if (curK == 1) { if (!st.sz || st.front() != skob[curJ][0]) { return false; } st.pop(); } else { st.push(c); } } return !st.sz; }
// 追加スタック数2 MyStack sortStack(MyStack stack) { MyStack ans(10), tmp(10); // 最小値を見つける while (!stack.isEmpty()) { int min = stack.peek(); while (!stack.isEmpty()) { int t = stack.peek(); stack.pop(); if (t <= min) { min = t; } tmp.push(t); } // 最小値を入れる while (!tmp.isEmpty()) { if (tmp.peek() == min) { ans.push(tmp.peek()); } else { stack.push(tmp.peek()); } tmp.pop(); } } return ans; }
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"); }
bool uTest( UnitTest *utest_p) { bool thrown_error = false; //Tested instance MyStack stack; stack.push(1); stack.push(2); UTEST_CHECK( utest_p, stack.size() == 2); int second = stack.pop(); int first = stack.pop(); UTEST_CHECK( utest_p, second == 2); UTEST_CHECK( utest_p, first == 1); UTEST_CHECK( utest_p, stack.size() == 0); try { stack.pop(); } catch ( MyStack::Error) { thrown_error = true; } UTEST_CHECK( utest_p, thrown_error); return utest_p->result(); }
//先序 void MyTree::PreTraverse(TreeNode* pNode) { // if(pNode == NULL) // { // return; // } // // printf("%d ", pNode->m_Data); // PreTraverse(pNode->m_pLeft); // PreTraverse(pNode->m_pRight); MyStack<TreeNode*> ss; TreeNode* pCur = pNode; do { while(pCur) { printf("%d ", pCur->m_Data); ss.push(pCur); pCur = pCur->m_pLeft; } if(!ss.IsEmpty()) { pCur = ss.pop(); pCur = pCur->m_pRight; } }while(pCur || !ss.IsEmpty()); }
void string_test() { string next; char c; MyStack<string> s; cout << "Enter a sentence or two\n"; // read from terminal word by word while (cin >> next) { // put latest word into stack s.push(next); // was that the last word on the line? c = cin.get(); if (c == '\n') break; else cin.putback(c); } cout << "Written backward that is:\n"; while (!s.empty()) cout << s.pop() << " "; cout << "\n"; }
void char_test() { char next; MyStack<char> s; cout << "Enter some text\n"; // read characters in one by one until newline reached cin.get(next); while (next != '\n') { // push latest character s.push(next); cin.get(next); } cout << "Written backward that is:\n"; // output all characters stored in stack while (!s.empty()) cout << s.pop(); cout << "\n"; }
//Driver code int main() { MyStack s; s.push(3); s.push(5); s.getMin(); s.push(2); s.push(1); s.getMin(); s.pop(); s.getMin(); s.pop(); s.peek(); return 0; }
void testStack() { MyStack s; for(int i = 1; i <= 10; i++) s.push(i); MyStack s2 = s; cout << "s:\n"; while (!s.empty()) cout << s.pop() << endl; MyStack s3; s3 = s; for(int i = 11; i <= 20; i++) s.push(i); cout << "s2:\n"; while (!s2.empty()) cout << s2.pop() << endl; cout << "Живи сме!\n"; }
void Expression::checkvp() { stk = new MyStack(exp.length()); for (int i = 0; i < exp.length(); ++i) { if (exp.at(i) == '{' || exp.at(i) == '(' || exp.at(i) == '[') { stk->push(exp.at(i)); } else if (exp.at(i) == '}' || exp.at(i) == ')' || exp.at(i) == ']') { if (exp.at(i) == '}') { char c = stk->get_top(); if (c == '{') { stk->pop(exp.at(i)); } else { cout << "Not well parenthesized!\n" return; } } if (exp.at(i) == ')') { char c = stk->get_top(); if (c == '(') { stk->pop(exp.at(i)); } else { cout << "Not well parenthesized!\n" return; } } if (exp.at(i) == ']') { char c = stk->get_top(); if (c == '[') { stk->pop(exp.at(i)); } else { cout << "Not well parenthesized!\n" return; } }
int main(){ MyStack currStack; currStack.push(10); currStack.push(3); currStack.push(12); currStack.push(13); currStack.push(14); currStack.push(1); currStack.push(8); cout << currStack.max() << endl; cout << currStack.min() << endl; cout << "----" << endl; int data; //data = currStack.pop(); //data = currStack.pop(); data = currStack.pop(); cout << data << endl; cout << currStack.max() << endl; cout << currStack.min() << endl; cout << "----" << endl; data = currStack.pop(); cout << data << endl; cout << currStack.max() << endl; cout << currStack.min() << endl; cout << "----" << endl; data = currStack.pop(); cout << data << endl; cout << currStack.max() << endl; cout << currStack.min() << endl; cout << "----" << endl; data = currStack.pop(); cout << data << endl; cout << currStack.max() << endl; cout << currStack.min() << endl; cout << "----" << endl; return 0; }
void MyTree::RemoveAll() { MyStack ss; TreeNode *pCurNode = m_pRoot; TreeNode *pLastNode = NULL; while (true) { while (pCurNode != NULL) { ss.push(pCurNode); pCurNode = pCurNode->m_pLeft; } if (ss.IsEmpty()) { break; } pCurNode = ss.pop(); if (pCurNode->m_pLeft == NULL && pCurNode->m_pRight == NULL) { pLastNode = pCurNode; } else if (pCurNode->m_pRight == NULL || pCurNode->m_pRight == pLastNode) { pLastNode = pCurNode; Del(pCurNode); pCurNode = NULL; continue; } else { ss.push(pCurNode); pCurNode = pCurNode->m_pRight; continue; } TreeNode *pDelNode = pCurNode; pCurNode = pCurNode->m_pRight; Del(pDelNode); } }
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; }
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; }
void enQueue(int x) { if(rear == len-1) cout << "Queue is full" << endl; else { if(isEmpty()) front = rear = 0; else rear++; S1.push(x); } }
/* Function: twoNumsProcess() * Usage: is called by sortTokenByStacks() or getFinalStacksResult() functions * -----------------------------------------------------------------------------------------// * Makes single calculation for two top numbers in stack, and return result to * stackNumbers back. * * * @param stackNumbers Stack of number values for current recursion * @param stackOperators Stack of operators for current recursion */ void twoNumsProcess(MyStack<double> &stackNumbers, MyStack<string> &stackOperators) { /* Stacks elements validation checking for calculating process */ if((stackNumbers.size() - stackOperators.size()) != 1) { cout << " - CHECK YOUR INPUT, NOT ENOUGH NUMBERS IN FORMULA!" << endl; failFlag = true; } else { /* Calculating process */ double num2 = stackNumbers.pop(); double num1 = stackNumbers.pop(); string thisOper = stackOperators.pop(); double result = singleCalculation(num1, thisOper, num2); stackNumbers.push(result); } }
// 追加スタック数1 MyStack sortStack1(MyStack stack) { MyStack ans(10); while(!stack.isEmpty()) { int t = stack.peek(); stack.pop(); /// 要素がansのtopより小さい間,ansからstackに戻す while (!ans.isEmpty() && ans.peek() < t) { stack.push(ans.peek()); ans.pop(); } ans.push(t); } return ans; }
void Graph<Tv, Te>::BCC(int v, int &clock, MyStack<int> &S){ //assert: 0 <= v < n hca(v) = dTime(v) = ++clock; status(v) = DISCOVERED; S.push(v); //v被发现并入栈 for(int u = firstNbr(v); -1 < u; u = nextNbr(v, u)) //枚举v的所有邻居u switch(status(u)){ //并视u的状态分别处理 case UNDISCOVERED: parent(u) = v; type(v,u) = TREE; BCC(u, clock, S); //从顶点u处深入 if(hca(u) < dTime(v)) //遍历返回后, 若发现u(通过回边)可指向v的真祖先 hca(v) = min( hca(v), hca(u) ); //则v亦必如此 else{ //否则,以v为关节点(u以下即是一个BCC, 且其中顶点此时正集中于栈s的顶部) while( v != S.pop() ); //依次弹出当前BCC中的节点,亦可根据实际需求转存至其他结构 S.push(v); //最后一个顶点(关节点)重新入栈---总计至多两次 } break; case DISCOVERED: type(v, u) = BACKWARD; //标记(v,u), 并按照"越小越高"的准则 if( u != parent(v) ) hca(v) = min(hca(v), dTime(u)); //更新hca(v) break; default: //VISITED(digraphs only) type(v, u) = (dTime(V) < dTime(u) ? FORWARD : CROSS); break; } status(v) = VISITED; //对v的访问结束 }
int main (void) { char num[] = "0123456789ABCDEF"; MyStack<char> *pStack = new MyStack<char>(50); int N = 10; int mod = 0; while (N != 0) { mod = N % 16; pStack->push(num[mod]); N /= 16; } pStack->stackTraverse(false); cout << endl; delete pStack; pStack = NULL; return 0; }
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; }
//中序 void MyTree::MidTraverse(TreeNode* pNode) { MyStack<TreeNode*> ss; TreeNode* pCur = pNode; do { while(pCur) { ss.push(pCur); pCur = pCur->m_pLeft; } if(!ss.IsEmpty()) { pCur = ss.pop(); printf("%d ", pCur->m_Data); pCur = pCur->m_pRight; } }while(pCur || !ss.IsEmpty()); }
void Driver(MyStack<T> &listObject) { T max; cout << "Welcome to MyStack , Lets Get you started \n\n "; cout << "Ok , Lets get one thing straight how big must the Stack be : "; cin >> max; cout << "\n\nThank you ... Ready Steady Go .. \n\n"; //instruc(); int choice; T value; listObject.setMax(max); do { instruc(); cin >> choice; switch (choice) { case 1: cout << "Enter Your Value : "; cin >> value; listObject.push(value); system("cls"); listObject.peek(); break; case 2: listObject.pop(value); system("cls"); listObject.peek(); break; } } while (choice < 3); }
int main() { MyStack S; Queue Q; Q.enQueue(5); Q.enQueue(6); Q.enQueue(1); Q.enQueue(2); Q.enQueue(3); Q.enQueue(4); Q.enQueue(7); Q.enQueue(9); Q.display(); while(!Q.isEmpty()) S.push(Q.deQueue()); while(!S.isEmpty()) Q.enQueue(S.pop()); Q.display(); }
int main(void){ MyStack<char> *pStack = new MyStack<char>(30); MyStack<char> *pNeedStack = new MyStack<char>(30); char str[] = "[()]]"; char currentNeed = 0; for(int i = 0; i < strlen(str);i++){ if(str[i]!=currentNeed){ pStack->push(str[i]); switch(str[i]){ case '[': if(currentNeed!=0){ pNeedStack->push(currentNeed); } currentNeed = ']'; break; case '(': if(currentNeed!=0){ pNeedStack->push(currentNeed); } currentNeed = ')'; break; default: cout<<"×Ö·û´®²»Æ¥Åä"<<endl; return 0; } }else{ char elem; pStack->pop(elem); if(!pNeedStack->pop(currentNeed)){ currentNeed = 0; } } } if(pStack->stackEmpty()){ cout<<"×Ö·û´®Æ¥Åä"<<endl; }else{ cout<<"×Ö·û´®²»Æ¥Åä"<<endl; } delete pStack; pStack = NULL; delete pNeedStack; pNeedStack = NULL; /*ÊýÖÆת»» char num[] = "0123456789ABCDEF"; MyStack<int> *pStack = new MyStack<int>(30); int N = 2016; int mod = 0; while(N != 0){ mod = N % OCTONARY; pStack->push(mod); N/=OCTONARY; } //pStack->stackTraverse(false); int elem = 0; while(!pStack->stackEmpty()){ pStack->pop(elem); cout<<num[elem]; } delete pStack; pStack = NULL; */ /* MyStack<char> *pStack = new MyStack<char>(5); pStack->push('h'); pStack->push('l'); pStack->stackTraverse(true); //pStack->clearStack(); pStack->stackTraverse(false); cout<<pStack->stackLength()<<endl; if(pStack->stackEmpty()){ cout<<"ջΪ¿Õ"<<endl; } if(pStack->stackFull()){ cout<<"ջΪÂú"<<endl; } delete pStack; pStack = NULL; */ system("pause"); return 0; }
/* Function: formulaStringScanning() * -----------------------------------------------------------------------------------------// * Recursively scanns formula string and sorts each token through stacks due to shunting-yard * algorithm. If "()" appear in this recursion it controls brackets condition. Detects two * pow, sqrt - additional library function in user formula. Breaks process due to global * failFlag. Returns double of calculated result. In case of process faults zero result * is returned, and fail message is shown. * * @param scanner Scanner for main formula string * @param bracketsOpenedBefore Brackets were opened before this recursion invocation * @param xValue Value for x variable in user equation */ double formulaStringScanning(TokenScanner& scanner, bool &bracketsOpenedBefore, double xValue) { if(failFlag) { return 0; //Global flag apearance } else { MyStack<double> stackNumbers;//Stacks for current recursion invocation MyStack<string> stackOperators; string token = ""; bool bracketsOpenedHere = false;//Rises if "(" appear in this recursion while(scanner.hasMoreTokens()) { token = scanner.nextToken(); if(token == "x") { /* Sabstitute x-token by user xValue param */ stackNumbers.push(xValue); } else if(token == "pow") { /* Lunches library pow function process */ stackNumbers.push(powFunction(scanner)); } else if(token == "sqrt") { /* Lunches library sqrt function process */ stackNumbers.push(sqrtFunction(scanner, xValue)); } else { //Brackets case if(token == "(") { bracketsOpenedHere = true; /* Calls new formula recursion for this scanner */ stackNumbers.push(formulaStringScanning(scanner, bracketsOpenedHere, xValue)); } else if(token == ")") { if(bracketsOpenedBefore) { //Brackets are closed correctly bracketsOpenedBefore = false;//it is end of this recursion break; } else { //Token is ")" and no bracketsOpenedBefore flag failFlag = true; //Brackets were opened cout << " - NOT OPENED BRACKETS! " << endl; break; //Break to show error to user } } else { if(failFlag)break; /* If no fails, and token is valid - lunches * Shunting-Yard sorting */ sortTokenByStacks(token, stackNumbers, stackOperators); } }//End of Brackets case else statement }//End of while(scanner.hasMoreTokens()) /* Shunting-yard final calculation */ if(bracketsOpenedBefore) { //If brackets haven't been closed in this recursion cout << " - NOT CLOSED BRACKETS! " << endl; failFlag = true; return 0; } else { if(failFlag) { //There were some other fails return 0; } else { /* Calculate main result, due to stacks, for current calculation stage. */ return getFinalStacksResult(stackNumbers, stackOperators); } } }//End of else statement (no failFlag at recursion start) }