Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FilmListe;
- import stdLib.In;
- public class Umfrage {
- private FilmListe filmListe;
- public Umfrage(In in) {
- filmListe = new FilmListe(in.readInt(), in.readString());
- in.readLine();
- FilmListe node = filmListe;
- while(!in.isEmpty()) {
- node.next = new FilmListe(in.readInt(), in.readString());
- node = node.next;
- in.readLine();
- }
- }
- /*
- * Der Film mit den meisten Stimmen wird gesucht, aus der Liste entfernt und zurückgegeben.
- */
- private FilmListe bestMovie(FilmListe liste) {
- if(liste == null)
- return new FilmListe(0,"");
- int maxStimmen = 0;
- FilmListe node = liste;
- for(; node != null; node = node.next)
- maxStimmen = (node.stimmen > maxStimmen) ? node.stimmen : maxStimmen;
- if(liste.stimmen == maxStimmen) {
- FilmListe erg = liste;
- return erg;
- }
- for(; liste.next != null; liste = liste.next)
- if(liste.next.stimmen == maxStimmen) {
- FilmListe erg = liste.next;
- liste.next = liste.next.next;
- erg.next = null;
- return erg;
- }
- return null;
- }
- /*
- * Kopie der Liste wird erstellt.
- */
- private FilmListe createCopy() {
- FilmListe alt = filmListe;
- FilmListe neu = new FilmListe(alt.stimmen, alt.titel);
- FilmListe ref = neu;
- alt = alt.next;
- for(; alt != null; alt = alt.next, neu = neu.next)
- neu.next = new FilmListe(alt.stimmen, alt.titel);
- return ref;
- }
- /*
- * Idee hier ist es die ursprüngliche Liste zu kopieren und via bestMovie immer den Film mit der höchsten Anzahl
- * an Stimmen in das Array zu packen. Dabei wird eben dieser Eintrag aus der Liste entfernt. Das führt man so lange aus bis entweder
- * das Array voll ist oder die Liste leer. Damit hat man alle Einträge direkt sortiert in seinem Array.
- */
- public FilmListe[] findTopTen() {
- FilmListe[] arr = new FilmListe[10];
- FilmListe copy = this.createCopy();
- for(int i = 0; i < 10; i++) {
- arr[i] = this.bestMovie(copy);
- if(copy == arr[i])
- copy = copy.next;
- }
- return arr;
- }
- public static void printList(FilmListe liste) {
- for(; liste != null; liste = liste.next)
- System.out.println(liste.stimmen + " " + liste.titel);
- }
- public static void main(String[] args) {
- In in = new In("datei.txt");
- Umfrage x = new Umfrage(in);
- FilmListe[] arr = x.findTopTen();
- for(int i = 0; i < arr.length; i++)
- System.out.println(arr[i].stimmen + ", " + arr[i].titel);
- }
- }
Add Comment
Please, Sign In to add comment