1, achieving source
public class SeqStack { Private Final int the MaxSize =. 8 ; Private int Top; // stack Private Object Stack []; / ** * Initialization * / public SeqStack () { the this .top = 0 ; the this .stack = new new Object [ the MaxSize]; } / ** * is empty * @param seqStack: to determine the stack * @return to true: empty false: non-empty * / Private BooleanisEmpty (SeqStack seqStack) { IF (seqStack.top <= 0 ) { return to true ; } return to false ; } / ** * is full * @param seqStack: To determine the stack * @return : to true: Full false: empty * / Private Boolean isFull (seqStack seqStack) { IF (seqStack.top> = the MaxSize) { return to true ; } return to false ; } / ** * Drawing *@param seqStack: to push the stack * @param Element: push data * / Private void stackPush (SeqStack seqStack, Element Object) { IF (isFull (seqStack)) { System.out.println ( "Unable to insert the stack is full " ); return ; } seqStack.stack [seqStack.top] = Element; System.out.println (Element +" is pushed onto the stack " ); seqStack.top ++ ; } / ** * the stack * @param seqStack: to pop the stack * / Private void stackPop (SeqStack seqStack) { IF (isEmpty (seqStack)) { System.out.println ( "empty stack out no element" ); return ; } seqStack.top - ; of System.out.print (seqStack.stack [seqStack.top] + "" ); } / ** * get the top element * @param seqStack: to get the top element of the stack * / Private void stackTop (SeqStack seqStack) { IF (isEmpty (seqStack)) { the System .out.println ( "empty stack, no stack element" ); return ; } System.out.println("取栈顶元素值 :"+ seqStack.stack[seqStack.top - 1]); } public static void main(String[] args) { SeqStack seqStack = new SeqStack(); seqStack.stackPop(seqStack); seqStack.stackTop(seqStack); for (int i = 0; i < 9; i++) { seqStack.stackPush(seqStack,i); } seqStack.stackTop(seqStack); int number = seqStack.top; System.out.print("元素出栈: "); for (int i = 0; i < number; i++) { seqStack.stackPop(seqStack); } } }
2, the test results
An empty stack elements no empty stack, without the top element 0 has the stack 1 have been pushed onto the stack 2 has been pushed onto the stack 3 has been pushed onto the stack 4 has been pushed onto the stack 5 has been pushed onto the stack 6 has been pushed onto the stack 7 has been pushed onto the stack the stack is full not insertion takes top element Found: 7 elements in the stack: 76543210