Skip to main content

Posts

Showing posts with the label queue in java

Data Structure and Algorithms : Queue Program in Java

Program: public class queuepgm { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; public queuepgm(int s) { maxSize = s; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; } public void enqueue(long j) { if(rear == maxSize-1) rear = -1; queArray[++rear] = j; nItems++; } public long dequeue() { long temp = queArray[front++]; if(front == maxSize) front = 0; nItems--; return temp; } public long peekFront() {     return queArray[front]; } public boolean isEmpty()       { return (nItems==0); } public boolean isFull() { return (nItems==maxSize); } public int size() { return nItems; } public static void main(String[] args) { queuepgm theQueue = new queuepgm(5); // queue holds 5 items theQueue.enqueue(10); theQueue.enqueue(20); theQueue.enqueue(30); theQueue.enqueue(40); System.out.println("Dequeue Values: FIFO"); while( !theQueue.isEmpty() ) ...