What is a stack structure:
Stack structure is classified from the operation of data, and the stack structure has special operation rules.
From the logical structure of data, the stack structure is actually a linear structure .
but!!!
From the storage structure of data, the stack structure can be divided into two categories:
sequential table structure: the data in the stack is saved sequentially with a contiguous set of memory units. In a program, you can define a
Specifies the size of the structure array to be used as the stack, the element with the ordinal 0 is the bottom of the stack, defining a top
The ordinal of the top of the stack.
chained stack structure: The value of each element in the stack is saved using a chained form. The list header (the head reference element) is the top of the stack,
The tail of the list (pointing to the null address) is the bottom of the stack.
stack structure follows : the principle of LIFO (last in Firt out, LIFO) processes node data. Example: Goods in warehouse, LIFO
The two basic operations of the stack structure (only the top of the stack can be accessed):
On- Stack (push): the operation to save data to the top of the stack. Before the stack operation, modify the top reference of the stack so that it moves up one
Element position, and then saves the data to the position referred to by the top of the stack.
out Stack (POP): The operation that pops up the data at the top of the stack. Make it point to the next element in the stack by modifying the top reference of the stack.
Attach a stack structure to the instance code ( see the code feeling comparison good understanding ~ ~):
1 import Java.util.Scanner;2 3 /*********************************************************************4 * *5 * Stack Structure Operation example *6 * *7 *********************************************************************/8 9 classdata3{Ten String name; One intAge ; A } - - classstacktype{ the StaticFinalintMaxLen = -; -data3[] data =Newdata3[maxlen+1];//Data Elements - intTop//Top of Stack - + Stacktype Stinit () { - Stacktype p; + if(P =NewStacktype ())! =NULL){//Application Stack Memory AP.top =0;//set stack top to 0 at returnP//returns a reference to the stack - } - return NULL; - } - //determine if the stack is empty - Boolean stisempty (Stacktype s) { in Boolean t; -t= (S.top = =0); to returnT; + } - //determine if the stack is full the Boolean stisfull (Stacktype s) { * Boolean t; $t = (S.top = =maxlen);Panax Notoginseng returnT; - } the //Empty Stack + voidStclear (Stacktype s) { AS.top =0; the } + //freeing the stack for space - voidStfree (Stacktype s) { $ if(s! =NULL){ $s =NULL; - } - } the //in-stack operation - intpushst (stacktype s,data3 data) {Wuyi if((s.top+1) >maxlen) { theSystem. out. Print ("Stack Overflow! \ n"); - return 0; Wu } -S.data[++s.top] = data;//put elements into the stack About return 1; $ } - //out of stack operation - DATA3 Popst (Stacktype s) { - if(S.top = =0){ ASystem. out. println ("stack is empty!"); +System.exit (0); the } - return(S.data[s.top]); $ } the //read stack top data the DATA3 Peekst (Stacktype s) { the if(S.top = =0){ theSystem. out. println ("stack is empty 2"); -System.exit (0); in } the return(S.data[s.top]); the } About } the /*-----------------------------Debugging-----------------------------------*/ the Public classP2_3 { the@SuppressWarnings ("Resource") + Public Static voidMain (string[] args) { -Stacktype st =NewStacktype (); theDATA3 data1 =NewDATA3 ();Bayi theStacktype stack = St. Stinit ();//Initialize Stack theScanner input =NewScanner (System.inch); -System. out. println ("into the stack operation:"); -System. out. println ("Enter the name age to proceed to the stack:"); the Do{ theDATA3 data =NewDATA3 (); theData.name =Input.next (); the - if(Data.name.equals ("0")){ the Break;//If you enter 0, exit the}Else{ theData.age =input.nextint ();94 St. Pushst (Stack, data); the } the} while(true); theString temp ="1";98 while(!temp.equals ("0")){ AboutData1 =St. Popst (stack); -System. out. printf ("The data for the stack is: (%s,%d) \ n", data1.name,data1.age);101temp =Input.next ();102 }103System. out. println ("The test is over! ");104St. Stfree (ST);//space occupied by the free stack the }106}
The common algorithm of Java in retreat cultivation--stack structure