Guest User

Untitled

a guest
Jul 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package FilmListe;
  2.  
  3. import stdLib.In;
  4.  
  5. public class Umfrage {
  6. private FilmListe filmListe;
  7.  
  8. public Umfrage(In in) {
  9. filmListe = new FilmListe(in.readInt(), in.readString());
  10. in.readLine();
  11. FilmListe node = filmListe;
  12. while(!in.isEmpty()) {
  13. node.next = new FilmListe(in.readInt(), in.readString());
  14. node = node.next;
  15. in.readLine();
  16. }
  17. }
  18. /*
  19. * Der Film mit den meisten Stimmen wird gesucht, aus der Liste entfernt und zurückgegeben.
  20. */
  21.  
  22. private FilmListe bestMovie(FilmListe liste) {
  23. if(liste == null)
  24. return new FilmListe(0,"");
  25. int maxStimmen = 0;
  26. FilmListe node = liste;
  27. for(; node != null; node = node.next)
  28. maxStimmen = (node.stimmen > maxStimmen) ? node.stimmen : maxStimmen;
  29.  
  30. if(liste.stimmen == maxStimmen) {
  31. FilmListe erg = liste;
  32. return erg;
  33. }
  34.  
  35. for(; liste.next != null; liste = liste.next)
  36. if(liste.next.stimmen == maxStimmen) {
  37. FilmListe erg = liste.next;
  38. liste.next = liste.next.next;
  39. erg.next = null;
  40. return erg;
  41. }
  42. return null;
  43. }
  44.  
  45. /*
  46. * Kopie der Liste wird erstellt.
  47. */
  48. private FilmListe createCopy() {
  49. FilmListe alt = filmListe;
  50. FilmListe neu = new FilmListe(alt.stimmen, alt.titel);
  51. FilmListe ref = neu;
  52. alt = alt.next;
  53. for(; alt != null; alt = alt.next, neu = neu.next)
  54. neu.next = new FilmListe(alt.stimmen, alt.titel);
  55. return ref;
  56. }
  57.  
  58. /*
  59. * Idee hier ist es die ursprüngliche Liste zu kopieren und via bestMovie immer den Film mit der höchsten Anzahl
  60. * 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
  61. * das Array voll ist oder die Liste leer. Damit hat man alle Einträge direkt sortiert in seinem Array.
  62. */
  63. public FilmListe[] findTopTen() {
  64. FilmListe[] arr = new FilmListe[10];
  65. FilmListe copy = this.createCopy();
  66. for(int i = 0; i < 10; i++) {
  67. arr[i] = this.bestMovie(copy);
  68. if(copy == arr[i])
  69. copy = copy.next;
  70. }
  71. return arr;
  72. }
  73.  
  74. public static void printList(FilmListe liste) {
  75. for(; liste != null; liste = liste.next)
  76. System.out.println(liste.stimmen + " " + liste.titel);
  77. }
  78.  
  79. public static void main(String[] args) {
  80. In in = new In("datei.txt");
  81. Umfrage x = new Umfrage(in);
  82. FilmListe[] arr = x.findTopTen();
  83. for(int i = 0; i < arr.length; i++)
  84. System.out.println(arr[i].stimmen + ", " + arr[i].titel);
  85. }
  86. }
Add Comment
Please, Sign In to add comment