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: