Package datastruct; import Java. util. arrays;/*** simulate the stack structure using Arrays: Post-in-first-out (LIFO) linear table structure * @ author stone * 06:34:49 */public class simulatestack <E> {public static void main (string [] ARGs) {simulatestack <string> SS = new simulatestack <string> (6); // simulatestack <string> SS = new simulatestack <string> (); system. out. println ("isempty:" + SS. isempty (); SS. push ("ABC"); SS. push ("EDF"); SS. push ("Ghi"); SS. push ("kkk "); System. out. println ("isfull:" + SS. isfull (); SS. push ("AAA"); SS. push ("eee"); SS. pushes ("777"); system. out. println ("isfull:" + SS. isfull (); system. out. println ("Peek:" + SS. peek (); system. out. println ("Search:" + SS. search (new string ("eee"); system. out. println ("Pop:" + SS. pop (); system. out. println ("Search:" + SS. search (new string ("eee"); system. out. println ("isempty:" + SS. isempty (); system. out. println ("isfu LL: "+ SS. isfull ();} private object [] marray; // Private int msize = 6; // Private int mcount of the internal array; // The data volume in the stack is private int MTop; // indicates the stack top position public simulatestack () {marray = new object [msize];} public simulatestack (INT size) {This. msize = size; marray = new object [msize];}/*** stack ** @ Param item */Public void push (E item) {If (! Isfull () {// if not full of marray [MTop ++] = item; // The stack top is 1, where the array is placed at 0 .. MTop is larger than the current location. 1 mcount = MTop; system. out. println (arrays. tostring (marray); system. out. println ("MTop = mcount =" + MTop);} else {system. out. println ("the stack is full, and the stack cannot be re-written:" + item) ;}/ *** check the top element of the stack, not stack * @ return */public e PEEK () {return (e) marray [MTop-1];}/*** output stack */public e POP () {If (! Isempty () {// if the object is not empty = marray [-- MTop]; mcount --; marray [MTop] = NULL; Return (e) object;} return NULL ;} /*** empty stack? */Public Boolean isempty () {If (mcount = 0) {return true;} return false ;} /*** determine whether the stack is full (without this function in the java stack, it uses vector, and the Capacity variable and growth factor are used in the vector to maintain the internal array, * If the current capacity is insufficient, add it to the Growth Factor. If the current capacity is not enough, set the old capacity to * 2 to determine the final capacity, and then use arrays. copy (array, newlength) * @ return */Public Boolean isfull () {If (mcount = msiz E) {return true;} return false;}/*** find the position of an element in the stack * @ Param OBJ * @ return */Public int search (e obj) {If (! Isempty () {for (INT I = 0; I <marray. length; I ++) {If (obj. equals (marray [I]) {return I + 1 ;}} return-1 ;}/ *** @ return number of elements in the stack */Public int size () {return mcount ;}}
Output result
Isempty: True
[ABC, null]
MTop = mcount = 1
[ABC, EDF, null]
MTop = mcount = 2
[ABC, EDF, Ghi, null]
MTop = mcount = 3
[ABC, EDF, Ghi, KKK, null, null]
MTop = mcount = 4
Isfull: false
[ABC, EDF, Ghi, KKK, AAA, null]
MTop = mcount = 5
[ABC, EDF, Ghi, KKK, AAA, eee]
MTop = mcount = 6
The stack is full and cannot be re-written: 777
Isfull: True
Peek: eee
Search: 6
Pop: eee
Search:-1
Isempty: false
Isfull: false