Java implements stacks and queues with linked lists

Source: Internet
Author: User

1, using the chain list to implement the stack

Package stack;/** * * @author DENGHB * */class link {public long ddata;public link next;public link (long dd) {dData = DD; }public void DisplayLink () {System.out.print (DData + "");}}    Class Linklist {private Link first;    Public linklist () {first = null;    public Boolean isEmpty () {return (first = = null);    public void Insertfirst (long dd) {link newLink = new Link (DD);    Newlink.next = First;    first = NewLink;    } public Long Deletefirst () {//Assume this is a non-empty linked list link temp = first;    first = First.next;    return temp.ddata;    } public void Displaylist () {Link current = first;    while (current = null) {Current.displaylink ();    current = Current.next;    } System.out.println (""); }}class linkstack {private linklist thelist;public linkstack () {thelist = new linklist ();} public void push (Long j) {Thelist.insertfirst (j);} Public long Pop () {return Thelist.deletefirst ();} public Boolean isEmpty () {return (Thelist.isempty ());} public void DisplaystAck () {System.out.print ("Stack (Top-->bottom):"); Thelist.displaylist ();}} public class Linkstackapp {public static void main (string[] args) {Linkstack thestack = new Linkstack (); Thestack.push (20); Thestack.push (+); Thestack.displaystack (); thestack.push; Thestack.push (+); Thestack.displaystack (); Thestack.pop (); Thestack.pop (); Thestack.displaystack ();}}

2, using the chain list to implement the queue

Package queue;/** * * @author DENGHB * */class link {public long ddata;public link next;public link (long dd) {dData = DD; }public void DisplayLink () {System.out.print (DData + "");}}    Class Firstlastlist {Private link first;private link last;    Public Firstlastlist () {first = null;    last = null;    public Boolean isEmpty () {return (first = = null);    public void Insertlast (long dd) {link newLink = new Link (DD);    if (IsEmpty ()) {first = NewLink;    } else {last.next = NewLink;    } last = NewLink;    } public Long Deletefirst () {//Assume this is a non-empty linked list long temp = First.ddata;    if (First.next = = null) {//If there is only one link last = null;    } first = First.next;    return temp;    } public void Displaylist () {Link current = first;    while (current = null) {Current.displaylink ();    current = Current.next;    } System.out.println (""); }}class linkqueue {private firstlastlist thelist;public linkqueue () {thelist = new firstlastlist ();} Public void Insert (Long j) {Thelist.insertlast (j);} Public long Remove () {return Thelist.deletefirst ();} public Boolean isEmpty () {return (Thelist.isempty ());} public void Displaystack () {System.out.print ("Stack (Top-->bottom):"); Thelist.displaylist ();}} public class Linkqueueapp {public static void main (string[] args) {linkqueue thequeue = new Linkqueue (); Thequeue.insert (20 ); Thequeue.insert (+); Thequeue.displaystack (); thequeue.insert; Thequeue.insert (+); Thequeue.displaystack (); Thequeue.remove (); Thequeue.remove (); Thequeue.displaystack ();}}

The output of the first source program:

Stack (Top-->bottom): 40 20
Stack (Top-->bottom): 80 60 40 20
Stack (Top-->bottom): 40 20

The output of the second source program:

Stack (Top-->bottom): 20 40
Stack (Top-->bottom): 20 40 60 80
Stack (Top-->bottom): 60 80

Java implements stacks and queues with linked lists

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.