mardi 21 avril 2015

DoublyLinkedList - Gerade zahlen ausgeben lassen

Hallo
als erstes das was ich benutzen darf..
Java Code:

  1. public class DoublyLinkedList<T>
  2. {
  3. private Element<T> first, last;
  4. private int size;
  5.  
  6. public DoublyLinkedList()
  7. {
  8. first = last = null;
  9. size = 0;
  10. }
  11.  
  12. public int size()
  13. {
  14. return size;
  15. }
  16.  
  17. public boolean isEmpty()
  18. {
  19. return size == 0;
  20. }
  21.  
  22. public void add( T content )
  23. {
  24. Element<T> e = new Element<T>( content );
  25. if ( isEmpty() )
  26. {
  27. first = last = e;
  28. }
  29. else
  30. {
  31. last.connectAsNext( e );
  32. last = e;
  33. }
  34. size++;
  35. }
  36.  
  37. public void showAll()
  38. {
  39. Element<T> current = first;
  40. while ( current != null )
  41. {
  42. if ( current.getContent() != null )
  43. {
  44. System.out.print( current.getContent().toString() );
  45. if ( current != last )
  46. {
  47. System.out.print(", ");
  48. }
  49. }
  50. current = current.getNext();
  51. }
  52. System.out.println();
  53. }
  54. public class Element<E>
  55. {
  56. private E content;
  57. private Element<E> previous, next;
  58.  
  59. public Element( E c )
  60. {
  61. content = c;
  62. previous = next = null;
  63. }
  64.  
  65. public E getContent()
  66. {
  67. return content;
  68. }
  69.  
  70. public void setContent( E c )
  71. {
  72. content = c;
  73. }
  74.  
  75. public boolean hasNext()
  76. {
  77. return next != null;
  78. }
  79.  
  80. public Element<E> getNext()
  81. {
  82. return next;
  83. }
  84.  
  85. public void disconnectNext()
  86. {
  87. if ( hasNext() )
  88. {
  89. next.previous = null;
  90. next = null;
  91. }
  92. }
  93.  
  94. public void connectAsNext( Element<E> e)
  95. {
  96. disconnectNext();
  97. next = e;
  98. if ( e != null )
  99. {
  100. e.disconnectPrevious();
  101. e.previous = this;
  102. }
  103. }
  104.  
  105. public boolean hasPrevious()
  106. {
  107. return previous != null;
  108. }
  109.  
  110. public Element<E> getPrevious()
  111. {
  112. return previous;
  113. }
  114.  
  115. public void disconnectPrevious()
  116. {
  117. if ( hasPrevious() )
  118. {
  119. previous.next = null;
  120. previous = null;
  121.  
  122. }
  123. }
  124.  
  125. public void connectAsPrevious( Element<E> e )
  126. {
  127. disconnectPrevious();
  128. previous = e;
  129. if ( e != null )
  130. {
  131. e.disconnectNext();
  132. e.next = this;
  133. }
  134. }
  135. }
  136. }


So meine 1. aufgabe ist es alle gerade zahlen auszugeben mit system.print out
das was ich versucht habe
Java Code:

  1.  
  2. public void showGerade()
  3. {
  4. Element<T> current = first;
  5. while(current!=null)
  6. {
  7. if(current.getContent()!= null && current.getContent()%2==0)
  8. {
  9. System.out.print( current.getContent().toString());
  10. if(current!=last)
  11. {
  12. System.out.print(", ");
  13. }
  14. }
  15. current=current.getNext();
  16. }
  17. System.out.println();
  18. }

aber die zeile mit dem vergleich will iwie nicht klappen " current.getContent()%2==0" ... kann einer mir vllt weiter helfen...
Die zweite aufgabe ist es das maximum zu suchen und das zu löschen mit dem löschen weis ich ja wie das geht ..aber wie finde ich das maximum..
Mfg


DoublyLinkedList - Gerade zahlen ausgeben lassen

0 commentaires:

Enregistrer un commentaire