C Programs Tutorials | IT Developer
IT Developer

Java Programs - Advanced



Share with a Friend

Queue Program on Array - Java Program

Queue is an entity which can hold a maximum of 100 integers. The queue enables the user to add integers from the rear and remove integers from the front.

Define a class Queue with the following details:

Class name: Queue
Data members/instance variables:
que[]: array to hold the integer elements.
size: stores the size of the array.
front: to point to the index of the front.
rear: to point to the index of the rear.

Member functions:
Queue(int m): constructor to initialize the data size the data size = m, front = 0, rear = 0.
void addElement(int v): to add integer from the rear if possible, else display the message “Overflow”.
int delete(): returns elements from front if present, otherwise displays the message “Underflow” and returns -9999.
void display(): displays the array elements.

Specify the class Queue giving details of only the functions void addElement(int) and int delete(). Assume that the other functions have been defined.

The main function and algorithm need not be written.

class Queue{ public void addElement(int v){ if(rear + 1 < size) que[++rear] = v; else System.out.println("Overflow"); } public int delete(){ int d = que[front]; if(front == 0 && rear == 0){ System.out.println("Underflow"); return -9999; } else{ front++; if(front > rear) front = rear = 0; return d; } } }

Output

 
OUTPUT :