Skip to content
Snippets Groups Projects
Commit 8598926f authored by Eckl, Máté's avatar Eckl, Máté
Browse files

4. labor. Félreértett 6. feladat kijavítva

parent 88782ba4
No related branches found
No related tags found
No related merge requests found
/*
In computer science, a priority queue is an abstract data type which is
like a regular queue or stack data structure, but where additionally each element
has a "priority" associated with it. In a priority queue, an element with high priority
is served before an element with low priority. If two elements have the same priority,
they are served according to their order in the queue.
https://en.wikipedia.org/wiki/Priority_queue
*/
import java.util.ArrayList;
import java.util.Collections;
......@@ -18,14 +27,13 @@ public class PQueue <T extends Comparable> implements Iterable<T>{
void push (T t)
{
f.add(t);
Collections.sort(f);
}
T top()
{
if(size()<1)
throw new EmptyQueueException("Nincs elem a listában");
return f.get(size()-1);
return (T) Collections.max(f);
}
T pop()
......@@ -41,6 +49,7 @@ public class PQueue <T extends Comparable> implements Iterable<T>{
}
public Iterator<T> iterator() {
Collections.sort(f, new ReverseComparator<T>());
return f.iterator();
}
}
import java.util.Comparator;
public class ReverseComparator<T extends Comparable> implements Comparator<T>{
public int compare(T t, T t1) {
return -1*t.compareTo(t1);
}
}
......@@ -12,7 +12,16 @@ public class Test {
}
System.out.println(q.size());
System.out.println(q.pop());
System.out.println(q.top());
System.out.println(q.pop());
System.out.println(q.pop());
// System.out.println(q.pop());//exceptiont dobna ami nekünk most jó
PQueue<Integer> s = new PQueue<>();
s.push(1); s.push(2); s.push(3); s.push(4);
for (Integer i : s) {
System.out.println(i);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment