=================================================//File name:linkqueue_demo//-------------------------------- ----------------------------------------------//author:common//class name: firstlastlist//attribute://Method: Class Firstlastlist_lo Ng{private link_long first;private link_long last;public firstlastlist_long () {//Constructor This.first = Null;this.last = null;} public Boolean isEmpty () {return (first = = null);} public void Insertfirst (long dd) {//from the head of the list insert Link_long NewLink = new Link_long (dd); if (IsEmpty ()) {last = newlink;// Do not change first}newlink.next = First;first = NewLink;} public void Insertlast (long dd) {//From the end of the list insert Link_long NewLink = new Link_long (dd); if (IsEmpty ()) {first = newlink;// Do not change Last}else{last.next = newlink;//Add a new element after last, and modify the previous position}last = newlink;//Note: When there is only one element, the insertion is to be given as NewLink} Public long Deletefirst () {Link_long temp = first;//Staging Firstif (First.next = = null) {///If there is only one element, the last is also assigned as Nulllast = null;} First = first.next;//Set next to Firstreturn temp.ddata;//return to the original first}public void Displaylist (){System.out.println ("List (first-->last):"); Link_long current = first;//is used to continuously change the position implementation traversal while (current! = null) {Current.displaylink (); current = Current.next;}}} Class Name: linkqueue//property://Method: Implement Queue class Linkqueue{private Firstlastlist_long thelist;public linkqueue with double-ended list () {// Constructor thelist = new Firstlastlist_long ();//Create a double-ended list object}public void push (Long j) {//From the end of the list, insert the new element at the tail thelist.insertlast ( j);} Public long Pop () {return Thelist.deletefirst ();//Popup from the head of the list, advanced elements are first ejected}public Boolean idempty () {return thelist.isempty ();} public void Displayqueue () {System.out.println ("Queue (Front-->rear)"); Thelist.displaylist ();}} Main class//function:linkqueue_demopublic class Linkqueue_demo {public static void main (string[] args) {//TODO auto-generated method Stub linkqueue thequeue = new Linkqueue (); Thequeue.push (n); Thequeue.push (+); Thequeue.push (+); thequeue.displayqueue (); Thequeue.pop (); Thequeue.pop (); Thequeue.displayqueue ();}}
Java data structure--using a double-ended linked list to implement a queue