first, stack of the stack and into the stack rules
--first in, after or after the first out of two, simple stack code display
/**
* @use Custom Stack
* @author lattice * * *
/class Mystack {
private object[] elements;
private int size=0;
Set the stack size
private static final int mystack_length =;
Public Mystack () {
elements=new object[mystack_length];
}
into the stack public
void push (Object obj) {
//open new stack space for storing new data
ensurecapacity ();
elements[size++]=obj;
}
Out Stack public
Object pop () {
if (size==0)
throw new Emptystackexception ();
Object result=elements[size-1];
elements[--size]=null;//empty expired references. Eleminate Absolete reference return result
;
}
/**
* Handle full stack situation *
*
private void ensurecapacity () {
if (elements.length = size)
elements = Arrays.copyof (elements, size+1);
}
third, the stack of Code analysis stack rules
1, out of the stack pop () and into the stack push ()
POPs () each time the top of the stack, which is the largest element of the subscript, is removed and the corresponding element of the subscript is destroyed (set to a null object)
Push () always puts the element above the top of the stack, which is the position of the subscript of the maximum subscript +1.
So the stack and into the stack rules to determine the stack can only be first into the order, like 10 cars running towards a dead-end cul-de-sac, the car in front is either finished before the car in the back and comes out, or just wait for the car in the back to come out. 2, ensurecapacity () to ensure that every time the listing of the contents of the stack has a position to put this new element.
Four, custom stack test and results
public static void Main (string [] args) {
string str= ' This is lattice testing ';
System.out.println (str);
Mystack stack=new mystack ();
for (int i=0;i<100;i++) {
stack.push (i);
}
for (int i=0;i<100;i++) {
if (i%10==0) {
System.out.println ("");
}
System.out.print (Stack.pop () + "");
}
Run results Run Results The result is
lattice testing-----------
The "
49 48 47 46 45 44 43 4" of the "a"-------- 2 (a) of
19 18 17 16 15 14 13 12 11 10 in A-G-M