int main(int argc, char *argv[]) { CQueue<int> q; q.appendTail(1); q.appendTail(2); cout << q.deleteHead() << endl; cout << q.deleteHead() << endl; q.appendTail(4); cout << q.deleteHead() << endl; // q.deleteHead(); CStack<int> s; s.push(1); s.push(2); s.push(3); cout << s.top() << endl; s.pop(); cout << s.top() << endl; s.pop(); s.pop(); // s.pop(); return 0; }
int main() { CStack<int> s; s.push(10); s.push(20); s.push(30); cout << s.size() << endl; cout << s.top() << endl; s.pop(); s.pop(); s.pop(); cout << s.top() << endl; }
// Convert an infix expression to postfix // PRE: expr is the infix expression to convert // output is a string for the output // POST: output is the postfix expression, returns 1 on // success or 0 on error bool SolveInfix(const char *input,char* output) { char op[2]; // "string" for the operator stack char expr[MAX_LENGTH+1]; // Modifible input string int length; // The current length of the output string [+1] CStack opStack; // Operation stack int x; // Index variable for reading input bool foundError; // 1 if error found, otherwise 0 // memcpy(expr,input,MAX_LENGTH*sizeof(char)); InsertZeros(expr); // Initialize some variables op[1]=0; length=0; foundError=0; output[0]=0; // Read each character of the expression for(x=0;expr[x];x++) { *op=expr[x]; // If we have a left paren, push it if(isLeftParen(*op)) { opStack.push(op); // If we have a number, push it } else if(isNumber(*op)) { do { output[length++]=expr[x++]; } while(isNumber(expr[x])); output[length++]=' '; output[length]=0; --x; // If we have an operator, pop lower precedence operators and push it } else if(isOperator(*op)) { while(!opStack.empty() && !isLeftParen(opStack.top()[0]) && isOperator(opStack.top()[0])<=isOperator(*op)) { if(!isOperator(opStack.top()[0])) foundError=1; output[length++]=opStack.pop()[0]; output[length]=0; } opStack.push(op); // If we have a right paren, pop operators between the parens } else if(isRightParen(*op)) { for(;;) { if(opStack.empty()) break; *op=opStack.pop()[0]; if(!*op || isLeftParen(*op)) break; if(!isOperator(*op)) { foundError=1; break; } output[length++]=*op; } output[length]=0; // If this happens, we have a syntax error (unless whitespace) } else { switch(*op) { case ' ': case '\t': case '\n': break; default: foundError=1; break; } } // No need to continue if we have errors if(foundError) break; } // Pop any remaining operations off the stack while(!opStack.empty()) { if(!isOperator(opStack.top()[0])) foundError=1; output[length++]=opStack.pop()[0]; output[length]=0; } return !foundError; }