Sequential stacks: Use a continuous set of memory to save the data in the stack, and define a top variable to hold the stack number.
The stack structure is the "LIFO" principle.
The most basic operation of the stack is two: push (into stack) + pop (out of Stack)
Import Java.util.Scanner; /** * @author Lilylee * @date April 25, 2017 * @time afternoon 4:25:15 * @Version 1.0 * @email Lilylee_1213@foxmai
l.com * */public class Mystack {public static void main (string[] args) {stack_type st=new stack_type ();
Stack_data data1=new stack_data (); Stack_type Stack =st. Stinit ();
Initialize stack Scanner sc=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Input name Age end with Name= ' 0 '");
Enter the stack element to end with 0 while (true) {Stack_data sdata=new stack_data ();
Sdata.name=sc.next ();
if (Sdata.name.equals ("0")) {break;}
Sdata.age=sc.nextint (); St.
Pushst (Stack, sdata);
String flag= "1";
System.out.println ("Pop stack End with 0")//Enter any non 0 elements to complete the stack flag=sc.next (); while (!) ( Flag.equals ("0"))) {data1=st.
Popst (stack);
if (data1==null) {break;}
System.out.println (data1.name+ "" +data1.age);
Flag=sc.next ();
} System.out.println ("End and free"); St.
Stfree (ST);
Class stack_data{String name;
int age; } class Stack_type{static final int maxlen=50;//define Maximum length of stack stack_data[] data=new stack_data[maxlen+1];
int top;
Stack_type Stinit () {Stack_type p;
if ((P=new stack_type ())!=null) {//application stack memory p.top=0;
return p;
return null;
Boolean stisempty (Stack_type s) {//To determine whether the stack is an empty Boolean t;
T= (s.top==0);
return t;
Boolean stisfull (Stack_type s) {//To determine whether the stack is full Boolean t;
T= (S.top==maxlen);
return t;
} void Stclear (Stack_type s) {//empty stack s.top=0;
} void Stfree (Stack_type s) {//release stack occupied space if (s!=null) {s=null; } int Pushst (Stack_type s,stack_data Data) {if (s.top+1) >maxlen) {return 0;}
Stack Overflow s.data[++s.top]=data;
return 1; } stack_data Popst (Stack_type s) {if (s.top==0) {System.out.println ("no elements!"); return null;}//Stack empty, operation failed return
(s.data[s.top--]); } stack_data Peekst (Stack_type s) {if (s.top==0) {system.exit (0);}
Stack is empty, operation failure return (S.data[s.top]);
}
}