The Java implementation sequence stack was learned earlier: http://www.cnblogs.com/lixiaolun/p/4644134.html
Next, learn Java to implement the chain stack.
Chain Stack Class Code:
Package Linkedstack;public class Linkstack {private Element base;private element top;class element{public Object data;pub Lic Element next; /** * Initialize stack * */void initstack () {top = new element (); base = new Element (); top.data=null;top.next=base;base.data=null;base. Next=null;} /** * into the stack * */void push (Object o) {Element e = new Element (); e.data = O;if (top.next==base)//first-time into stack operation {e.next=base;top.next=e;} Else{e.next=top.next;top.next=e;}} /** * out of Stack * */void pop () {if (top.next==base) {System.out.println ("There are no elements in the stack!") ");} Else{system.out.println ("stack operation" +top.next.data); top.next=top.next.next;}} /** * Print Stack * */void print () {System.out.print ("Print stack:"); Element temp =top;while (temp.next!=base) {System.out.print (temp.next.data+ "\ t"); temp =temp.next;} System.out.println ();}}
Link stack test class code:
Package Linkedstack;public class Linkstackmain {public static void main (string[] args) {Linkstack lstack = new Linkstack () ; Lstack.initstack (); Lstack.pop (); Lstack.push (1); Lstack.push (2); Lstack.push (3); Lstack.push (4); Lstack.print (); Lstack.pop (); Lstack.pop (); Lstack.print (); Lstack.pop (); Lstack.pop (); Lstack.print (); Lstack.pop ();}}
Java implementation chain Stack