下のような連結リストの場合に、while文の動作を繰り返したい。
void bubbleSort() { Cell c1 = header.next; Cell c2 = c1.next; while (c2 != end) { if ( (Integer)c1.data > (Integer)c2.data ) { swap(c1,c2); } c1 = c1.next; c2 = c2.next; }
上のコードで実行すると、{0,3,4,1,2}が{0,3,1,2,4}になります。whileの部分を繰り返して{0,1,2,3,4}にしたいです。
void bubbleSort() { Cell c = header.next; Cell i = c.next for (int i=1 ; i<= 100; ++i) { while (i != end) { if ( (Integer)c.data > (Integer)i .data ) { swap(c,i); } i = i.next; } } }
上のように、iというCellをおいてCell 1, Cell 2, ... , Cell 100のように数字の名前のCellをつくって、while文を繰り返すことは可能なのでしょうか?
バブルソートのロジックと、上の関数の処理をよく見比べて何が必要なのか考えれば答えが出る気がします。
あなたの回答
tips
プレビュー