Javaを利用しています。
常に昇順となる双方向リストのプログラムを作成をしたいんですが、
条件として、「数値を1個毎に、双方向リストの昇順となる位置へ追加する」「数値はファイルから入力する」が与えられています。
双方向リストは自分で実装しなくてはいけません。
ファイルから入力・出力まではできるのですが、双方向リストへの入れ方がわかりません。
java
1import java.io.BufferedReader; 2 3public static void main(String[] args){ 4 String file1 = "ファイル名"; 5 String file2 = "ファイル名"; 6 7 /* 8 * file1からデータを入力 9 */ 10 11 try { 12 FileReader in = new FileReader(file1); 13 BufferedReader rd = new BufferedReader(in); 14 String line; 15 while ((line = rd.readLine()) != null) { 16 17 } 18 rd.close(); 19 } 20 catch (FileNotFoundException e) { 21 System.out.println(file1+"ファイルが見つかりません。"); 22 } 23 catch (IOException e) { 24 System.out.println(e); 25 } 26 27 28 29 /* 30 * file2に結果を出力 31 */ 32 try { 33 BufferedReader rd =new FileReader("ファイル名")); 34 FileWriter out = new FileWriter("ファイル名"); 35 BufferedWriter wr = new BufferedWriter(out); 36 rd.readLine(); 37 out.plintln(); 38 rd.close(); 39 out.close(); 40 } 41 catch (IOException e) { 42 System.out.println(e); 43 } 44 } 45}
java
1public class Lesson{ 2 static DoublyListInt head; // ポインタ(前方) 3 static DoublyListInt tail; // ポインタ(後方) 4 static DoublyListInt ptr; // 作業用ポインタ 5 static int n; // データ 6 static int i; // ループカウンタ 7 static int k; // 作業用カウンタ 8 static int count; // 比較回数 9 static Random rnd = new Random(); 10public static void main(String[] args) { 11 head = null; 12 tail = null; 13 ptr = null; 14 n = 10; 15 count = 0; 16 17 18 DoublyListInt list = new DoublyListInt(count, head); 19String file1 = "ファイル名"; 20String file2 = "ファイル名"; 21 22//file1からデータを入力 23try { 24FileReader fr = new FileReader(file1); 25BufferedReader br = new BufferedReader(fr); 26String line; 27while ((line = br.readLine()) != null) { 28 for (int i = 0; i < 10; i++) { 29 30 push(line); 31 } 32 33} 34br.close(); 35} 36catch (FileNotFoundException e) { 37System.out.println(file1+"ファイルが見つかりません。"); 38} 39catch (IOException e) { 40System.out.println(e); 41} 42 43bubbleSort(); 44System.out.println(list); 45 46//file2に結果を出力 47try { 48BufferedReader br = new BufferedReader(new FileReader(file1)); 49PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(file2))); 50br.readLine(); 51pr.println(list); 52br.close(); 53pr.close(); 54} catch (IOException e) { 55System.out.println(e); 56} 57} 58 59static int rand(int x) { 60 int n; 61 n = rnd.nextInt(x); 62 return n; 63} 64 65private static void push(int p) { 66 head = new DoublyListInt(p, head); 67 if (tail == null) { 68 tail = head; 69 } 70} 71 72static void bubbleSort() { 73 for (int k = 0; k < n; k++) { 74 ptr = head; 75 for (int j = n; k < j; j--) { 76 boolean ex = compare(); 77 if (ex) { 78 swap(ptr); 79 } 80 count++; 81 if (ptr != null && ptr.next != null) { 82 ptr = ptr.next; 83 } 84 } 85 } 86} 87 88static boolean compare() { 89 if (ptr != null && ptr.next != null) { 90 int s = ptr.value; 91 int t = ptr.next.value; 92 if (s < t) { 93 System.out.println(s + "<" + t); 94 return true; 95 } else { 96 System.out.println(s + ">" + t); 97 } 98 99 } 100 return false; 101} 102 103static void swap(DoublyListInt y) { 104 DoublyListInt a = null; 105 DoublyListInt b = null; 106 107 if ((y != null) && (y.next != null)) { 108 a = y; 109 b = a.next; 110 111 if (a.prev == null) { 112 head = b; 113 } else { 114 a.prev.next = b; 115 } 116 117 if (b.next == null) { 118 tail = a; 119 } else { 120 b.next.prev = a; 121 } 122 123 b.prev = a.prev; 124 a.prev = b; 125 126 a.next = b.next; 127 b.next = a; 128 129 System.out.println(); 130 System.out.println("結果"); 131 showdata(); 132 ptr = y.prev; 133 } 134} 135static void showdata() { 136 ptr = tail; 137 for (int i = 0; i < n; i++) { 138 if (ptr != null) { 139 int v = ptr.value; 140 System.out.println(i + "\t" + v); 141 ptr = ptr.prev; 142 } 143 } 144 System.out.println(); 145} 146 147}
自分なりにやってみた結果
DoublyListInt を型に解決できません
型 Lesson のメソッド push(int) は引数 (String) に適用できません
とでできました。
どこをどのように直したらいいでしょうか。