Java Data Structure Series--Queue (3): Chain storage structure of queue and its implementation

Source: Internet
Author: User

Package Queue.linkqueue;public class Node {//Data Field object element;//pointer domain node next;//header node Initialize public node {This.next = Next;} Non-head node initialization public node (Object element, node next) {this.element = Element;this.next = Next;}}

***************************************************************************************

Package Queue.linkqueue;public class Linkqueue {node front;//head node node rear;//tail node int size;//Queue Size//Initialize an empty queue public linkque UE () {front = new Node (null); rear = Front;size = 0;} The size of the queue public int size () {return size;} Determines whether the queue is empty public boolean isEmpty () {return size = = 0;} into the queue public void enQueue (int data) {node node = new Node (data, null); rear.next = node;rear=node;size++;} Out of queue public Object DeQueue () {if (IsEmpty ()) {throw new Indexoutofboundsexception ("Queue is Empty");} Node node = front.next;//Gets a node Front.next = node.next;//The first node of the head node points to the next node of the first node if (node = = rear) {///If there is only one element in the queue, then the R The ear points to the head node rear = front;} Size--;return node.element;} Get the opponent element, do not delete public Object Getfront () {if (IsEmpty ()) {return null;} else {return front.next.element;}}  The elements in the print queue public void traverse () {if (IsEmpty ()) {System.out.println ("null"),} else {node current = front.next;//The present node for (node node = current; node = null; node = node.next) {System.out.print (node.element + "");}} System.out.println ();}}


Java Data Structure Series--Queue (3): Chain storage structure of queue and its implementation

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.