Data Structure queue implementation (JAVA), queue java

Source: Internet
Author: User

Data Structure queue implementation (JAVA), queue java

For StackElement, see stack implementation.

Package com. lip. datastruture. stack; public class Queue <T> {private StackElement <T> obj; // private int size for saving data; // number of elements public Queue () {this. size = 0;} // 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;} // public boolean deQueue () {if (this. size> 0) {this. obj = this. obj. getNextElement (); this. size --; return true;} return false;} // obtain the public T getFirst () {if (obj! = Null) return obj. getData (); return null;} // get the last element in 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 <20; 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 <20; I ++) {System. out. println (queue. getFirst (); queue. deQueue ();}}}
Running result:

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.