Java code for queue using linked lists

Source: Internet
Author: User

Using linked lists to implement JavaCode

Strong http://blog.csdn.net/microtong

The queuenode class is used to represent a linked list node. queuelinked is the queue implemented by the linked list,

Here, front is the first pointer, and rear is the team's tail pointer. The team leaves the team and enters the team's end.

 

/** <Br/> * queuelinked. java <br/> */<br/> package cn.edu. UIBE. oop; </P> <p>/** <br/> * linked list node class <br/> * @ author Tongqiang <br/> */<br/> class queuenode {< br/> object data; // data stored on the node <br/> queuenode next; // pointer to the next node </P> <p> Public queuenode () {<br/> This (null, null); <br/>}</P> <p> Public queuenode (Object Data) {<br/> This (data, null); <br/>}</P> <p> Public queuenode (Object Data, queuenode next) {<br/> This. Data = data; <br/> This. next = next; <br/>}< br/>/** <br/> * queue implemented by one-way linked list <br/> * @ author Tongqiang <br/> * /<br/> public class queuelinked {<br/> queuenode front; // first team pointer <br/> queuenode rear; // team end pointer </P> <p> Public queuelinked () {<br/> This. rear = NULL; <br/> This. front = NULL; <br/>}< br/>/** <br/> * append an object to the end of the queue. <br/> * @ Param OBJ object <br/> */ <br/> Public void enqueue (Object OBJ) {<br/> // If the queue is Null <br/> If (Rear = NULL & Front = NULL) {<br/> rear = new queuenode (OBJ); <br/> front = rear; <br/>} else {<br/> queuenode node = new queuenode (OBJ); <br/> rear. next = node; <br/> rear = rear. next; <br/>}< br/>/** <br/> * the first target of the team to be outputted <br/> * @ return the target of the team, if the queue is empty, null is returned. <br/> */<br/> Public object dequeue () {<br/> // If the queue is empty <br/> If (front = NULL) {<br/> return NULL; <br/>}< br/> // if only one object is left in the queue <br/> If (Front = rear & Rear! = NULL) {<br/> queuenode node = front; <br/> rear = NULL; <br/> front = NULL; <br/> return node. data; <br/>}< br/> Object OBJ = front. data; <br/> front = front. next; <br/> return OBJ; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> queuelinked q = new queuelinked (); <br/> q. enqueue ("James"); <br/> q. enqueue ("LITH"); <br/> q. enqueue ("Zhao Wu"); <br/> q. enqueue ("Wang Yi"); <br/> for (INT I = 0; I <4; I ++) {<br/> system. out. println (Q. dequeue (); <br/>}< br/>

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.