Advertisement
porteno

Queue

Dec 10th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1.  
  2. public class Queue<T> {
  3. private Node<T> first;
  4. private Node<T> last;
  5. public Queue() {
  6. this.first = null;
  7. this.last = null;
  8. }
  9.  
  10. public boolean isEmpty() {
  11. return this.first == null;
  12. }
  13.  
  14. public void insert(T x) {
  15. Node<T> tmp = new Node<T>(x);
  16. if(this.last ==null)
  17. this.first = tmp;
  18. else
  19. this.last.setNext(tmp);
  20. this.last = tmp;
  21. }
  22.  
  23. public T remove() {
  24. T x = this.first.getValue();
  25. this.first = this.first.getNext();
  26. if(this.first == null)
  27. this.last = null;
  28. return x;
  29. }
  30.  
  31. public T top() {
  32. return this.first.getValue();
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement