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