/** ********************* * Push an element. * * @param paraObject The given object. * @return Success or not. ********************* */ publicbooleanpush(Object paraObject) { if (depth == MAX_DEPTH) { System.out.println("Stack full."); returnfalse; } // Of if
data[depth] = paraObject; depth++;
returntrue; }// Of push
2. 出栈
描述
从栈顶去除一个元素.
输入
无
输出
返回一个类为Object的对象为之前的栈顶元素.
若栈为空则打印 "Nothing to pop." 并返回 '\0' .
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** ********************* * Pop an element. * * @return The object at the top of the stack. ********************* */ public Object pop() { if (depth == 0) { System.out.println("Nothing to pop."); return'\0'; } // of if
ObjectresultObject= data[depth - 1]; depth--;
return resultObject; }// Of pop
3. 栈判空
描述
通过代码中栈指针的位置判断当前栈中是否还有元素.
输入
无
输出
栈中还有元素返回 false .
栈中无元素返回 true .
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** ********************* * Is the stack empty? * * @return True if empty. ********************* */ publicbooleanisEmpty() { if (depth == 0) { returntrue; } // Of if
/** ********************* * Construct an empty sequential list. ********************* */ publicObjectStack() { depth = 0; data = newObject[MAX_DEPTH]; }// Of the first constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString=""; for (inti=0; i < depth; i++) { resultString += data[i]; } // Of for i
return resultString; }// Of toString
/** ********************* * Push an element. * * @param paraObject The given object. * @return Success or not. ********************* */ publicbooleanpush(Object paraObject) { if (depth == MAX_DEPTH) { System.out.println("Stack full."); returnfalse; } // Of if
data[depth] = paraObject; depth++;
returntrue; }// Of push
/** ********************* * Pop an element. * * @return The object at the top of the stack. ********************* */ public Object pop() { if (depth == 0) { System.out.println("Nothing to pop."); return'\0'; } // of if
ObjectresultObject= data[depth - 1]; depth--;
return resultObject; }// Of pop
/** ********************* * Is the stack empty? * * @return True if empty. ********************* */ publicbooleanisEmpty() { if (depth == 0) { returntrue; } // Of if
returnfalse; }// Of isEmpty
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { ObjectStacktempStack=newObjectStack();
for (charch='a'; ch < 'm'; ch++) { tempStack.push(Character.valueOf(ch)); System.out.println("The current stack is: " + tempStack); } // Of for i
char tempChar; for (inti=0; i < 12; i++) { tempChar = ((Character) tempStack.pop()).charValue(); System.out.println("Poped: " + tempChar); System.out.println("The current stack is: " + tempStack); } // Of for i }// Of main }// Of class ObjectStack
while (!tempStack.isEmpty() || tempNode != null) { if (tempNode != null) { // Store for output. tempOutputStack.push(Character.valueOf(tempNode.value)); tempStack.push(tempNode); tempNode = tempNode.rightChild; } else { tempNode = (BinaryCharTree) tempStack.pop(); tempNode = tempNode.leftChild; } // Of if } // Of while
// Now reverse output. while (!tempOutputStack.isEmpty()) { System.out.print("" + tempOutputStack.pop() + " "); } // Of while }// Of postOrderVisitWithStack