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