自定义简单字符栈 java版
简单定义StackChar
package dataStructure;
public class StackChar {
public int size;
public char charStack[];
public int top;
StackChar( ){
}
public StackChar(int size){
this.size = size;
this.charStack = new char[size];
this.top = -1; //指向栈顶元素
}
public int getSize() {
return this.size;
}
public int getLenght() {
return this.top+1;
}
public int pushIn( char ch) {
if(this.top+1>=this.size) {
this.extendSize();
System.out.println("ran this.extendSize();");
//return -1; //bug,-solved
}
this.charStack[++this.top]=ch;
return this.top;
}
public char popOut() {
if(this.top<0) {
return ;
}
return this.charStack[this.top--]; //先出栈再--
}
public char getTop() {
return this.charStack[this.top];
}
public void PrintTopDown() {
for(int i=this.top;i>=0;i--) {
System.out.print(this.charStack[i]);
}
System.out.println();
}
public void printButtomUp() {
for(int i=this.top;i>=0;i--) {
System.out.print(this.charStack[i]);
}
System.out.println();
}
public void reverseStack() {
char temp;
for(int i=0;i<(this.top+1)>>1;i++) {
temp = this.charStack[i];
this.charStack[i] = this.charStack[this.top-i];
this.charStack[this.top-i] = temp;
}
}
public int extendSize(int addSize ) {
this.size+=addSize;
char[] temp = new char[this.size];
for( int i = 0; i <= this.top; i++ ) {
temp[i] = this.charStack[i];
}
this.charStack = temp;
System.out.print(temp.toString());
return this.size;
}
public int extendSize() {
this.size += this.size*0.618;
char[] temp = new char[this.size];
for( int i = 0; i <= this.top; i++ ) {
//top不动
temp[i] = this.charStack[i];
}
this.charStack = temp;
return this.size;
}
public int clearStack(){
this.top = -1;
return this.top;
}
}
简单测试StackChar
package dataStructure;
import java.util.Arrays;
public class Test_Strack {
public static void main(String args[]) {
char[] chTest = {
a,b,c,d,e,f,g,h,i,j,
k,l,m,n,o,p,q,r,s,t,
u,v,w,x,y,z,1};//26
System.out.println(Arrays.toString(chTest));
StackChar buff = new StackChar(15);
System.out.println("buff.size="+buff.size);
for( int i = 0 ; i<chTest.length; i++) {
//OK
buff.pushIn(chTest[i]); //测试pushIn()、extendSize() bug,-solved
}//赋初值
System.out.println("buff.size="+buff.size);
for( int i = 0; i < buff.getLenght(); i++ ) {
//测试getlength() OK
System.out.print( buff.charStack[i]+", " );
}
System.out.println("
buff.popOut():"+buff.popOut()); //测试popOut() OK
buff.PrintTopDown(); //测试PrintTopDown()
System.out.println();
for( int i = 0; i < buff.getLenght(); i++ ) {
System.out.print( buff.charStack[i]+"," );
}
buff.pushIn(z);
buff.reverseStack(); //测试reverseStack():奇数27 OK
System.out.println("
ran:
" + "buff.pushIn(z);
" + "buff.reverseStack();");
buff.PrintTopDown();
buff.reverseStack();
System.out.println("
popOut():"+buff.popOut());
buff.reverseStack();//测试reverseStack():偶数26 OK
buff.PrintTopDown();
System.out.println();
}
}
测试结果
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, 1] buff.size=15 ran this.extendSize(); ran this.extendSize(); buff.size=38 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, 1, buff.popOut():1 zyxwvutsrqponmlkjihgfedcba a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, ran: buff.pushIn(z); buff.reverseStack(); abcdefghijklmnopqrstuvwxyzz popOut():z abcdefghijklmnopqrstuvwxyz
下一篇:
数据结构—红黑树和二叉搜索树
