1.定义具有通用性的对象栈,依靠强制类型转换, 支持不同的数据类型.。
对象栈代码:
package com.two;
public class ObjectStack {
public static final int MAX_DEPTH = 10;// the depth
int depth;// the actual depth.
Object[] data;// the data;
public ObjectStack() {
depth = 0;
data = new Object[MAX_DEPTH];
}// of constructor
public String toString() {
String resultString = "";
for (int i = 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
*/
public boolean push(Object paraObject) {
if (depth == MAX_DEPTH) {
System.out.println("stack full.");
return false;
}// of if
data[depth] = paraObject;
depth++;
return true;
}// of push
/**
* pop an elememt
* @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
Object resultObject = data[depth - 1];
depth--;
return resultObject;
}// Of pop
public boolean isEmpty() {
if (depth == 0) {
return true;
}// of if
return false;
}// of isEmpty
public static void main(String[] args) {
ObjectStack tempStack = new ObjectStack();
for (char ch = 'a'; ch < 'm'; ch++) {
tempStack.push(new Character(ch));
System.out.println("The current stack is: " + tempStack);
}// of for ch
char tempChar;
for (int i = 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
测试对象栈结果:
The current stack is: a
The current stack is: ab
The current stack is: abc
The current stack is: abcd
The current stack is: abcde
The current stack is: abcdef
The current stack is: abcdefg
The current stack is: abcdefgh
The current stack is: abcdefghi
The current stack is: abcdefghij
stack full.
The current stack is: abcdefghij
stack full.
The current stack is: abcdefghij
Poped: j
The current stack is: abcdefghi
Poped: i
The current stack is: abcdefgh
Poped: h
The current stack is: abcdefg
Poped: g
The current stack is: abcdef
Poped: f
The current stack is: abcde
Poped: e
The current stack is: abcd
Poped: d
The current stack is: abc
Poped: c
The current stack is: ab
Poped: b
The current stack is: a
Poped: a
The current stack is:
Nothing to pop.
Poped:
The current stack is:
Nothing to pop.
Poped:
The current stack is:
2.中序遍历代码:
public void inOrderVisitWithStack() {
ObjectStack tempStack = new ObjectStack();
BinaryCharTree tempNode = this;
while (!tempStack.isEmpty() || tempNode != null) {
if (tempNode != null) {
tempStack.push(tempNode);
tempNode = tempNode.leftChild;
} else {
tempNode = (BinaryCharTree) tempStack.pop();
System.out.println("" + tempNode.value + " ");
tempNode = tempNode.rightChild;
}// of if
}// of while
}// of inOrderVisitWithStack
测试用例:
char[] tempCharArray = {'A','B','C','D','E','F'};
int[] tempIndicesArray = {0,1,2,4,5,12};
BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray,tempIndicesArray);
System.out.println("\r\nIn-order visit with stack:");
tempTree2.inOrderVisitWithStack();
运行结果:
In-order visit with stack:
B
D
A
E
F
C