Java data structure--using a double-ended linked list to implement a queue

Source: Internet
Author: User

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

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.