FIFO: Advanced First Out
Storage unit:
Public class The Node {/* element has two parts: a reference to the next element of the element */ Object data ; // data fields // pointer field Public Node () {} Public Node (Object data,node next) { this. data=data; this. next=next;} }
Realize:
/*** Created by yaming * Chain Queue---chained storage implementation*/ Public classLinkqueue {PrivateNode first;//Queue header, you can delete PrivateNode last;//end of queue, can be inserted Private intsize; /*** is empty *@return */ Public BooleanIsEmpty () {returnSize==0; } /*** length *@return */ Public intsize () {returnsize; } /*** Queue *@paramData *@return */ Public Booleanpush (Object data) {node node=NewNode (data,NULL); //If the chain queue is still an empty chain queue if(First = =NULL) { First=node; Last= First;//only one node, front, rear, point to that node}Else{Last.next= node;//let the tail node next point to the new nodelast = node;//New node as the new tail node} size++; return true; } /*** Out of the team *@return */ PublicObject Pull () {node node=First ; First=First.next; Node.next=NULL; Size--; returnNode.data; } /*** Queue Tail element *@return */ PublicObject GetLast () {if(IsEmpty ()) {return NULL; } returnLast.data; } PublicObject GetFirst () {if(IsEmpty ()) {return NULL; } returnFirst.data; } Public voidClear () { First=NULL; Last=NULL; Size=0; } /*** Traversal *@return */ PublicString Queue () {if(IsEmpty ()) {return"[]"; }Else{StringBuilder StringBuilder=NewStringBuilder ("["); for(Node current=first;current!=NULL; current=Current.next) {Stringbuilder.append (current.data.toString ()+", "); } intLength=stringbuilder.length (); returnStringbuilder.delete (Length-2,length). Append ("]"). toString (); } }}
Queue----Java Implementation