Queue implementation in Java
March 12, 2013 Leave a comment
Queues operate in FIFO model.
Queues are also used in various other data structures, some of them are
- searching graphs
- printer queues in our machines
- keystroke data that is typed onto the keyboard
Efficiency of queue is that it performs insert and remove operations in O(1) complexity.
package algorithm.queue; /** * @author ntallapa * */ public class TNQueue { private int size; private int[] queueArr; private int front = -1; private int rear = -1; private int totalItems; public TNQueue(int s) { size = s; queueArr = new int[s]; } public void insert(int i) { rear++; System.out.println("Inserting "+i); queueArr[rear] = i; totalItems++; } public int remove() { front++; totalItems--; System.out.println("Removing "+queueArr[front]); return queueArr[front]; } public boolean isFull() { return (totalItems == size); } public boolean isEmpty() { return (totalItems == 0); } } package algorithm.queue; /** * @author ntallapa * */ public class TNQueueClient { /** * @param args */ public static void main(String[] args) { TNQueue tnq = new TNQueue(3); if(!tnq.isFull()) tnq.insert(1); if(!tnq.isFull()) tnq.insert(2); if(!tnq.isFull()) tnq.insert(3); if(!tnq.isFull()) tnq.insert(4); else System.out.println("Queue is full, cannot insert element"); if(!tnq.isEmpty()) tnq.remove(); if(!tnq.isEmpty()) tnq.remove(); if(!tnq.isEmpty()) tnq.remove(); if(!tnq.isEmpty()) tnq.remove(); else System.out.println("Queue is empty, cannot remove element"); } }
Output:
Inserting 1
Inserting 2
Inserting 3
Queue is full, cannot insert element
Removing 1
Removing 2
Removing 3
Queue is empty, cannot remove element
Recent Comments