Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HMQueue {
- private static QueueNode tail = null, head = null;
- public static int dequeue() {
- if (head == null) {
- return -1;
- }
- QueueNode temp = head;
- head = head.next;
- if (head == null) {
- tail = null;
- }
- return temp.data;
- }
- public static void enqueue(int item) {
- QueueNode node = new QueueNode(item);
- if (head == null) {
- head = node;
- }
- else {
- tail.next = node;
- }
- tail = node;
- }
- public static int peek() {
- if (head == null) {
- return -1;
- }
- return head.data;
- }
- public static boolean isEmpty() {
- return tail == null && head == null;
- }
- }
- ----------------------------------------------------------------
- class QueueNode {
- int data;
- QueueNode next;
- public QueueNode(int data) {
- this.data = data;
- this.next = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement