Implementation of the queue of data structures (JAVA)

Source: Internet
Author: User

Stackelement see the implementation of the stack

Package Com.lip.datastruture.stack;public class Queue<t>{private stackelement<t> obj;//save Data Private int size;//number of elements public Queue () {this.size = 0;} Into the queue public boolean enQueue (T data) {if (data! = NULL) {stackelement<t> tempelement = obj;if (obj! = null) {while (obj . getnextelement () = null) {obj = Obj.getnextelement ();} obj. setnextelement (new stackelement<t> (data)); obj=tempelement;} else obj=new stackelement<t> (data); This.size++;return true;} return false;} Out of Queue public boolean deQueue () {if (This.size > 0) {this.obj = This.obj.getNextElement (); This.size--;return true;} return false;} The element that gets the first into the queue public T GetFirst () {if (obj! = null) return Obj.getdata (); return null;} Get the last element into the queue public T getlast () {stackelement<t> tempelement = obj;if (tempelement! = null) {while ( Tempelement.getnextelement ()! = null) {tempelement = Tempelement.getnextelement ();} return Tempelement.getdata ();} return null;} public int size () {return this.size;} public static void Main(string[] args) {queue<integer> Queue = new queue<integer> (); for (int i = 0; i <; i++) {queue.enqueue (i);} System.out.println ("First:" + Queue.getfirst ()); System.out.println ("Last:" + queue.getlast ()); System.out.println ("Size" + queue.size ()); for (int i = 0; i <; i++) {System.out.println (Queue.getfirst ()); Queue.dequeue ();}}}
Operation Result:

Implementation of the queue of data structures (JAVA)

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.