From 88782ba46149fe0f31182d02797654103544cab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eckl=2C=20M=C3=A1t=C3=A9?= <ecklm@sch.bme.hu> Date: Tue, 29 Sep 2015 09:45:52 +0200 Subject: [PATCH] =?UTF-8?q?4.=20labor=20marad=C3=A9k=20feladatok=20befejez?= =?UTF-8?q?ve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 4. labor/PQueue/src/EmptyQueueException.java | 17 ++++++++ 4. labor/PQueue/src/PQueue.java | 46 ++++++++++++++++++++ 4. labor/PQueue/src/Test.java | 18 ++++++++ 3 files changed, 81 insertions(+) create mode 100644 4. labor/PQueue/src/EmptyQueueException.java create mode 100644 4. labor/PQueue/src/PQueue.java create mode 100644 4. labor/PQueue/src/Test.java diff --git a/4. labor/PQueue/src/EmptyQueueException.java b/4. labor/PQueue/src/EmptyQueueException.java new file mode 100644 index 0000000..f9ebb44 --- /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 0000000..80a41b3 --- /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 0000000..68afa02 --- /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()); + } + +} -- GitLab