0% found this document useful (0 votes)
56 views5 pages

Package Import Public Class Public Static Void: "Insert" "Delete" "Display" "Destroy" "Exit"

This document defines a QueueADT class that implements a queue data structure using an array. It contains methods for inserting, deleting, displaying, and destroying queue elements. The main method creates a queue, displays a menu, and allows the user to insert, delete, display, destroy elements or exit by selecting different menu options in a loop until exit is selected.

Uploaded by

juztine dofitas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

Package Import Public Class Public Static Void: "Insert" "Delete" "Display" "Destroy" "Exit"

This document defines a QueueADT class that implements a queue data structure using an array. It contains methods for inserting, deleting, displaying, and destroying queue elements. The main method creates a queue, displays a menu, and allows the user to insert, delete, display, destroy elements or exit by selecting different menu options in a loop until exit is selected.

Uploaded by

juztine dofitas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy