You are on page 1of 5

package test;

import javax.swing.*;

public class QueueADT {

public static void main(String[] args) {

String[] choices = {"Insert", "Delete","Display",


"Destroy","Exit"};
Queue q = new Queue();
String input;
q.destroy();

do{
input = (String)JOptionPane.
showInputDialog(null, "Select: "
+"\n", "Menu", JOptionPane.QUESTION_MESSAGE,
null, choices, choices[0]);
switch (input){
case "Insert":
int i;
i = Integer.parseInt(JOptionPane.
showInputDialog(null, "Enter"
+ " data to insert: ",0));
q.insert(i);
break;
case "Delete":
if (q.empty())
JOptionPane.showMessageDialog(null, "Queue
underflow","",
JOptionPane.WARNING_MESSAGE);
else {
int z = q.delete();
JOptionPane.showMessageDialog(null, "data
deleted="+z,"",
JOptionPane.INFORMATION_MESSAGE);
}
break;
case "Display":
q.display();
break;
case "Destroy":
q.destroy();
break;
case "Exit":
System.exit(0);
break;
}
}while(input!="Exit");
}

}
package test;

import javax.swing.*;

public class Queue {


String c = "";
int a[];
int front , rear;

Queue(){
a = new int [5];
front = rear = 1;
}
Queue (int size){
a = new int [size];
front = rear = -1;
}
void insert (int x){
int p;
p=(rear +1)%a.length;
if (p == front)
JOptionPane.showMessageDialog(null,
"Queue Overflow","",
JOptionPane.WARNING_MESSAGE);
else {
rear = p;
a[rear] = x;
if (front == -1) front = 0;
}
}
boolean empty(){
if (front==-1) return true;
else return false;
}
int delete(){
int x = a[front];
if (front == rear) front = rear = -1;
else
front = (front+1)%a.length;
return x;
}
void display(){
if (front==-1)
JOptionPane.showMessageDialog(null,
"Queue Underflow", "",
JOptionPane.WARNING_MESSAGE);
else {
JOptionPane.showMessageDialog(null,
"Elements in the Queue are: \n" +disp(),
"Display",JOptionPane.INFORMATION_MESSAGE);
int i = front;
while (i != rear){
i = (i+1)%a.length;
}
for (int aa = 1; aa<a.length;aa++){
if (a[aa] == 0) {break;}
System.out.println(a[aa]);
}
System.out.println("----------");
}
}

String disp(){
String aa = "";
for (int ii = front; ii<rear+1; ii++){
aa=aa +a[ii]+" ";
}
return aa;
}
void destroy(){
front = rear = -1;
}
}
INSERT

DISPLAY

DELETE -> DISPLAY


DESTROY->DISPLAY

EXIT

You might also like