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(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<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; }