Java Collection Framework-implementing stack data structures with LinkedList

Source: Internet
Author: User

/* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.