Advertisement
Vladislav8653

normal queue

Aug 7th, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. class HMQueue {
  2.     private static QueueNode tail = null, head = null;
  3.  
  4.     public static int dequeue() {
  5.         if (head == null) {
  6.             return -1;
  7.         }
  8.         QueueNode temp = head;
  9.         head = head.next;
  10.         if (head == null) {
  11.             tail = null;
  12.         }
  13.         return temp.data;
  14.     }
  15.  
  16.     public static void enqueue(int item) {
  17.         QueueNode node = new QueueNode(item);
  18.         if (head == null) {
  19.             head = node;
  20.         }
  21.         else {
  22.             tail.next = node;
  23.         }
  24.         tail = node;
  25.     }
  26.  
  27.     public static int peek() {
  28.         if (head == null) {
  29.             return -1;
  30.         }
  31.         return head.data;
  32.     }
  33.  
  34.     public static boolean isEmpty() {
  35.         return tail == null && head == null;
  36.     }
  37.  
  38. }
  39.  
  40. ----------------------------------------------------------------
  41. class QueueNode {
  42.     int data;
  43.     QueueNode next;
  44.     public QueueNode(int data) {
  45.         this.data = data;
  46.         this.next = null;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement