Skip to content

Commit

Permalink
queue review & PriorityQueue created
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Neto committed Sep 25, 2023
1 parent dabc4ac commit 18d757b
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 11 deletions.
Binary file modified build/libs/DataStructures-1.1-snapshot.jar
Binary file not shown.
67 changes: 67 additions & 0 deletions src/main/java/net/jneto/dataStructures/Comparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.jneto.dataStructures;

public interface Comparator<ITEM>{
int compare(ITEM t1, ITEM t2);
}

/**
* EXAMPLE USAGE - String
* Priority: More length more Priority
*/
class BiggerStringLengthComparator implements Comparator<String> {
@Override
public int compare(String t1, String t2) {
if (t1.length() < t2.length()) {
return 1;
} else if (t1.length() > t2.length()) {
return -1;
}
return 0;
}
}
/**
* EXAMPLE USAGE - String
* Priority: More length more Priority
*/
class LessStringLengthComparator implements Comparator<String> {
@Override
public int compare(String t1, String t2) {
if (t1.length() < t2.length()) {
return -1;
} else if (t1.length() > t2.length()) {
return 1;
}
return 0;
}
}

/**
* EXAMPLE USAGE - Integer
* Priority: Bigger value more Priority
*/
class BiggerIntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer t1, Integer t2) {
if (t1 < t2) {
return 1;
} else if (t1 > t2) {
return -1;
}
return 0;
}
}
/**
* EXAMPLE USAGE - Integer
* Priority: Less value more Priority
*/
class LessIntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer t1, Integer t2) {
if (t1 < t2) {
return 1;
} else if (t1 > t2) {
return -1;
}
return 0;
}
}
80 changes: 71 additions & 9 deletions src/main/java/net/jneto/dataStructures/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ public Main(){
*/
public static void main(String[] args) {
output("Stack");
stackDemonstration();
arrayStackDemo();
output("Queue");
queueDemonstration();
arrayQueueDemo();
output("Bag");
bagDemonstration();
arrayBagDemo();
output("ArrayList");
listDemonstration();
arrayListDemon();

output("ArrayList additional methods ");
advancedArrayListStructureDemo();
output("Priority queue Array-min-heap based ");
priorityQueueDemo();

}

private static void queueDemonstration() {
private static void arrayQueueDemo() {
Queue<String> structure = new Queue<String>();
structure.add("a");
print(structure);
Expand Down Expand Up @@ -73,7 +76,7 @@ private static void queueDemonstration() {

}

private static void stackDemonstration(){
private static void arrayStackDemo(){
Stack<String> structure = new Stack<String>();
structure.add("a");
print(structure);
Expand Down Expand Up @@ -115,7 +118,7 @@ private static void stackDemonstration(){
structure.remove();
print(structure);
}
private static void bagDemonstration(){
private static void arrayBagDemo(){
Bag<String> structure = new Bag<String>();
structure.add("a");
print(structure);
Expand Down Expand Up @@ -158,7 +161,7 @@ private static void bagDemonstration(){
print(structure);
}

private static void listDemonstration(){
private static void arrayListDemon(){
ArrayList<String> structure = new ArrayList<String>();
structure.add("a");
print(structure);
Expand Down Expand Up @@ -201,7 +204,6 @@ private static void listDemonstration(){
print(structure);
}


/**
* A list have more complex methods to-do
*/
Expand Down Expand Up @@ -264,6 +266,66 @@ private static void advancedArrayListStructureDemo(){
print("Queue into List: " + test2.peek().show());

}

/**
* How to use Priority Queue demonstration
* and implements Compare Interface demonstration to
* use in any Objects
*/
private static void priorityQueueDemo(){
Comparator<String> stringComparator = new Comparator<String>(){ //how to make your comparator system for any Object
@Override
public int compare(String t1, String t2) {
if (t1.length() < t2.length()) { //If bigger - more priority
return 1;
} else if (t1.length() > t2.length()) {//else - less priority
return -1;
}

return 0; //zero if it's equals
}
};
Comparator<String> cp = new BiggerStringLengthComparator(); //another way into Comparator Interface example
PriorityQueue<String> pq = new PriorityQueue<String>(stringComparator);
pq.add("n");
pq.add("nome maior AAAAAAAAAa");
pq.add("b");
pq.add("nome maior CCCCCCCCCc");
pq.add("d");
pq.add("e");
pq.add("f");
print(pq.peek());
print(pq);
print(pq.show());
print(pq.remove());
print(pq.show());
print(pq.remove());
print(pq.show());
print(pq.remove());
print(pq.show());
print(pq.remove());
print(pq.show());
print(pq.peek());
print(pq.remove());
print(pq.show());
pq.add("c");
print(pq.show());
print(pq.remove());
print(pq.peek());
print(pq.remove());
print(pq.show());
print(pq.peek());
Comparator<Integer> compint = new BiggerIntegerComparator();
PriorityQueue<Integer> pqint = new PriorityQueue<Integer>(compint);

pqint.add(1);
pqint.add(2);
pqint.add(33);
pqint.add(4);


print(pqint.show());
}
private static void output(String name){
System.out.println("/*******************************************************");
System.out.println("/* " + name + " Demonstration");
Expand Down
Loading

0 comments on commit 18d757b

Please sign in to comment.