/* Use LinkedList to implement Queue (FIFO) and Stack (advanced out-of-the-first-out) Stacks: LIFO queue: FIFO */import java.util.*;/* queue: LinkedList is encapsulated to realize queue FIFO feature */class Duilie {private LinkedList Link;duilie () {link=new linkedlist ();} public void Myadd (Object obj) {link.addfirst (obj); SOP (Link.getfirst ());} Public Object MyGet () {return link.removelast ();} public Boolean isEmpty () {return link.isempty ();} public static void Sop (Object obj) {System.out.println (obj);}} /* Stack: encapsulates linkedlist to implement stack LIFO features */class duizhan{private LinkedList Link;duizhan () {link=new linkedlist ();} public void Myadd (Object obj) {link.addfirst (obj); SOP (Link.getfirst ());} Public Object MyGet () {return Link.removefirst ();} public Boolean isEmpty () {return link.isempty ();} public static void Sop (Object obj) {System.out.println (obj);}} Class Linkedlisttest{public static void Main (string[] args) {Duilie ();d Uizhan ();} public static void Duilie () {Duilie dl=new duilie (); SOP ("-------------------queue---------------------");d L.myadd (" Hello.. 1 ");d L.myadd (" Hello.. 2 ");d L.myadd (" Hello.. 3 ");d L.myadd ("Hello.. 4 "), SOP (" queue out Order "),//sop (DL), while (!dl.isempty ()) {SOP (Dl.myget ());} SOP ("---------------------End-------------------");} public static void Duizhan () {Duizhan dz=new Duizhan (); SOP ("-------------------stack---------------------");d Z.myadd (" Hello.. 1 ");d Z.myadd (" Hello.. 2 ");d Z.myadd (" Hello.. 3 ");d Z.myadd (" Hello.. 4 "), SOP (" Stack Out Order "),//sop (DZ), while (!dz.isempty ()) {SOP (Dz.myget ());} SOP ("---------------------End-------------------");} public static void Sop (Object obj) {System.out.println (obj);}}
Java Collection Framework-implementing stack data structures with LinkedList