Stack overview stack is a special linear table. It defines and features a restricted linear table stack. • It defines a linear table that is only inserted or deleted at the end of the table, table tail-stack top, table header-stack bottom, empty table without elements is called empty stack • features: the structure of the first, second, Second, First, Second, and second (LIFO) Stacks is shown in: operations on Linear tables mainly include: (1) clearing (HEAP) stacks (2) determining whether empty (3) number of elements (4) Incoming stacks (5) outgoing stacks (6) the top element interface of the stack is used to define the abstract data type of the Queue. The Queue interface is as follows: [java] package stack;/*** (HEAP) stack * @ author Administrator **/public interface Stack {/*** clear Stack */public void clear (); /***** elements of the inbound stack * @ param obj */public void push (Object obj);/***** elements of the outbound stack * @ return Result */public Object pop ();/*** determines whether it is null * @ return */public boolean isEmpty (); /*** calculate the number of elements * @ return number of elements */public int size (); /*** get the top element of the stack * @ return top element of the stack */public Object peek ();} order (HEAP) stack structure model top pointer of the stack top, point to the empty position at the top of the actual stack. The initial value is 0. The initial size of the stack is Mtop = 0, and the stack is empty. In this case, the stack is out, underflow, and top = M. When the stack is full, the stack is overflow ); source code [java] package stack;/*** sequence (HEAP) Stack * @ author Administrator **/public class ArrayStack implements stack {private static int DEFAULT_SIZE = 100; private int Top; object array []; public ArrayStack () {Top = 0; array = new Object [DEFAULT_SIZE];} public boolean isEmpty () {return 0 = Top ;} public void expand () {Object [] newArr Ay = new Object [2 * array. length]; for (int I = 0; I <array. length; I ++) {newArray [I] = array [I];} array = newArray;}/* public void expand () {try {Object [] newArray = new Object [2 * DEFAULT_SIZE]; for (int I = 0; I <array. length; I ++) {newArray [I] = array [I];} array = newArray;} catch (OutOfMemoryError e) {System. out. println ("error in expand of Stack class! "); // E. printStackTrace ();} DEFAULT_SIZE = 2 * DEFAULT_SIZE;} */public void push (Object obj) {if (Top = array. length) {expand () ;}array [Top] = obj; Top ++;} public Object pop () {if (0 = Top) throw new IllegalStateException (); object val = array [-- Top]; array [Top] = null; return val;} public void clear () {for (int I = 0; I <array. length; I ++) {array [I] = null; Top = 0 ;}} public Object peek () {If (0 = Top) throw new IllegalStateException (); return array [Top-1];} public int size () {return Top;} public String toString () {String s = "["; for (int I = Top-1; I> = 0; I --) {s = s + array [I]; s = s + "," ;}s = s + "]"; return s ;}} chain (HEAP) stack structure model source code [java] package stack; /*** chained (HEAP) stack Node * @ author luoweifu **/class Node {Object data; // data Element Node next; // post-drive Node public Node () {this (null );} Public Node (Object data) {this. data = data; this. next = null;}/*** chain (Stack) Stack, headless Node * @ author Administrator **/public class LinkStack implements Stack {private Node top; // stack top pointer private int size; // stack size public LinkStack () {top = null; size = 0 ;}@ Override public void clear () {top = null; size = 0 ;}@ Override public void push (Object obj) {Node p = new Node (obj); if (top = null) {top = p;} Else {p. next = top; top = p;} size ++ ;}@ Override public Object pop () {Node p = top; top = top. next; size --; return p. data ;}@ Override public boolean isEmpty () {if (size = 0) return true; else return false ;}@ Override public int size () {return size ;} @ Override public Object peek () {return top. data;} public String toString () {StringBuilder sb = new StringBuilder ("["); Node p = top; If (p = null) {sb. append ("");} else {do {sb. append (p. data + ",");} while (p = p. next )! = Null);} sb. append ("]"); return sb. toString () ;}} test (HEAP) stack [java] package stack; public class Test {/*** Test Stack * @ param args */public static void main (String [] args) {// stack Stack stack = new ArrayStack (); stack stack = new LinkStack (); for (int I = 0; I <10; I ++) {stack. push (I);} System. out. println (stack. toString (); Object a = stack. pop (); System. out. println (a + stack. toString (); stack. push (20); Obj Ect B = stack. peek (); System. out. println (B + stack. toString (); stack. clear (); System. out. println ("data quantity:" + stack. size () + "isEmpty? "+ Stack. isEmpty () + "data:" + stack. toString () ;}} result [9, 8, 7, 6, 5, 4, 3, 2, 1, 0,] 9 [8, 7, 6, 5, 4, 3, 2, 1, 0,] 20 [20, 8, 7, 6, 5, 4, 3, 2, 1, 0,] data quantity: 0 isEmpty? True data: []