CompleteCodeAs follows:
Package COM. xu. main; import Java. util. role; public class p4_1 {/*** @ function: stack structure operation * @ Author: * @ Date: 2012-10-16 */public static void main (string [] ARGs) {stacktype ST = new stacktype (); data Data = new data (); stacktype stack = ST. stinit (); inputs input = new partition (system. in); system. out. println ("inbound stack operation:"); system. out. println ("input name age for Stack import:"); do {data d = new data (); D. name = input. next (); If (D. name. equals ("0") {Break; // If the input is 0, exit} else {d. age = input. nextint (); ST. pushst (stack, d) ;}} while (true); string temp = "1"; system. out. println ("out-of-stack operation: any non-zero key out-of-stack operation:"); temp = input. next (); While (! Temp. equals ("0") {DATA = ST. popst (stack); system. out. printf ("data from the stack is: (% s, % d) \ n", Data. name, Data. age); temp = input. next ();} system. out. println ("test ended! "); ST. stfree (ST) ;}} class data {string name; int age;} class stacktype {static final int maxlen = 50; data [] DATA = new data [maxlen + 1]; // data element int top; // stack top @ suppresswarnings ("UNUSED") stacktype stinit () // initialize stack {stacktype P; if (P = new stacktype ())! = NULL) {P. top = 0; return P;} return NULL;} Boolean stisempty (stacktype s) // judge whether the stack is empty {Boolean t; t = (S. top = 0); Return t;} Boolean stisfull (stacktype s) // judge whether the stack is full {Boolean t; t = (S. top = maxlen); Return t;} void stclear (stacktype s) // clear the stack {S. top = 0;} void stfree (stacktype s) // release the space occupied by the stack {If (s! = NULL) {S = NULL;} int pushst (stacktype S, data) // The inbound stack operation {If (S. top + 1)> maxlen) {system. out. println ("stack overflow! "); Return 0;} s. data [++ S. top] = data; return 1;} data popst (stacktype s) // The output stack operation {If (S. top = 0) {system. out. println ("Stack is empty! "); System. exit (0);} return (S. data [S. top --]);} data peekst (stacktype s) // read the top data of the stack {If (S. top = 0) {system. out. println ("Stack is empty! "); System. Exit (0);} return S. Data [S. Top];}
Execution result: