diff --git a/4. labor/PQueue/src/EmptyQueueException.java b/4. labor/PQueue/src/EmptyQueueException.java new file mode 100644 index 0000000000000000000000000000000000000000..f9ebb44a13eae492c340507715020625509e3699 --- /dev/null +++ b/4. labor/PQueue/src/EmptyQueueException.java @@ -0,0 +1,17 @@ + +public class EmptyQueueException extends ArrayIndexOutOfBoundsException{ + public EmptyQueueException() + { + super("Üres a sor"); + } + + public EmptyQueueException(String err) + { + super(err); + } + + public EmptyQueueException(int i) + { + super(i); + } +} diff --git a/4. labor/PQueue/src/PQueue.java b/4. labor/PQueue/src/PQueue.java new file mode 100644 index 0000000000000000000000000000000000000000..80a41b3e5d8bdb34118bf0bb80dedc904d1bdc87 --- /dev/null +++ b/4. labor/PQueue/src/PQueue.java @@ -0,0 +1,46 @@ + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; + +public class PQueue <T extends Comparable> implements Iterable<T>{ + private ArrayList<T> f = null; + + public PQueue() { + f = new ArrayList<>(); + } + + int size() + { + return f.size(); + } + + 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); + } + + T pop() + { + T top = this.top(); + f.remove(top); + return top; + } + + void clear() + { + f.clear(); + } + + public Iterator<T> iterator() { + return f.iterator(); + } +} diff --git a/4. labor/PQueue/src/Test.java b/4. labor/PQueue/src/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..68afa02168ea27aaf633ff4fe5115a958e028dcb --- /dev/null +++ b/4. labor/PQueue/src/Test.java @@ -0,0 +1,18 @@ + +public class Test { + public static void main(String args[]) + { + PQueue<String> q=new PQueue<>(); + q.push("alma"); + q.push("körte"); + q.push("kiwi"); + for(String s: q) + { + System.out.println(s); + } + System.out.println(q.size()); + System.out.println(q.pop()); + System.out.println(q.top()); + } + +}