fork download
  1. public class ListElement{
  2.  
  3. private ListElement right;
  4. private int content;
  5.  
  6. // Konstruktor der Klasse ListElement
  7. public ListElement(ListElement right, int content){
  8. this.right = right;
  9. this.content = content;
  10. }
  11.  
  12. //Hier fehlen Getter und Setter
  13. //...
  14. public void setRight(ListElement right){
  15. this.right = right;
  16. }
  17. public void setContent(int content){
  18. this.content = content;
  19. }
  20.  
  21. public ListElement getRight(){
  22. return right;
  23. }
  24. public int getContent(){
  25. return content;
  26. }
  27.  
  28. private static ListElement start;
  29. public static void main(String[] args){
  30.  
  31. start = new ListElement(new ListElement(new ListElement (new ListElement(new ListElement(null,4),9),2),3),
  32. 8);
  33. sortList(start);
  34. testReturnSmallest();
  35. }
  36.  
  37. //Gibt das Element der Liste mit dem kleinsten Inhalt zurueck
  38. public static ListElement returnSmallest(ListElement start){
  39. ListElement temp = start;
  40. ListElement smallest = start;
  41. while (temp != null){
  42. if (smallest.getContent() > temp.getContent())
  43. smallest = temp;
  44. temp = temp.getRight();
  45. }
  46. return smallest;
  47. }
  48.  
  49. // das ist nur ein Versuch, um die Sortuerungsalgorithm zu schreiben, aber funktuniert nicht
  50.  
  51. /*Vertauscht die Inhalte der Eingabeliste, so dass sie sortiert sind und gibt
  52. das Startelement der neuen Liste zurueck
  53. */
  54. public static ListElement sortList(ListElement start){
  55. int temp = returnSmallest(start).getContent();
  56. int t2 = start.getContent();
  57. while(start.getRight() != null){
  58. returnSmallest(start.getRight()).setContent(t2);
  59. start.setContent(temp);
  60. start= start.getRight();
  61. System.out.println(start.getContent());
  62. }
  63. return start;
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70. // Test Smallest Value Method
  71. public static void testReturnSmallest(){
  72. System.out.println("The smallest value is: " + returnSmallest(start).getContent());
  73. }
  74.  
  75. /*Test Sort Method
  76. public static void testSort(){
  77. System.out.println(sortList(list).getContent());
  78. } */
  79. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class ListElement is public, should be declared in a file named ListElement.java
public class ListElement{
       ^
1 error
stdout
Standard output is empty