2012-03-26 19 views
0

iamはtxtファイルから得意先レコードを表示し、昇順で表示することになっています。最初の部分がありますが、sorting.hereを把握できません。ソート順にリストを取得するためのメソッドpublicボイド追加(顧客newNode、int型ダミー)実装されたリンクリストをソート

public void add(Customer newNode, int dummy) 

    { 
    if (head == null) // The first node 
    { 
    head = tail = this; 
    head.setData(newNode); size=1; 
    return; 
    } //************need to figure this out 
    CustomerList t = new CustomerList(); 
    head.setNext(temp); 
     getHead().getNext(); 
     head = temp; 

     //this is the part am trying to figure out 

    ++size; 
    } // add 
// Append the new node to the end of list 
public void add(Customer newNode) 
    { 
    if (head == null) // The first node 
     { 
     head = tail = this; 
     head.setData(newNode); 
     size=1; 
     return; 
     } 
    CustomerList temp = new CustomerList(newNode); 
    tail.setNext(temp); 
    getHead().getNext(); 
    tail = temp; 
    ++size; 
    } // add 

// retrieve a specific node by index 
// The index starts with 0 
public Customer get(int which) 
    { 
    if (which > size-1) 
     return null; 
    if (size < 0) 
     return null; 
    CustomerList temp = head; 
    for (int k=0; k < size; ++k) 
     { 
     if (which == k) 
      break; 
     temp = temp.getNext(); 
     } 
    return temp.getData(); 
    } // get 
+1

コレクションやアルゴリズムなどの「標準的なJava」を使用することは許されていますか、それともあなた自身で行うことはできますか? – John3136

答えて

1

使用Collections.sort(リスト)。そして、彼らはイテレータ()メソッドを使用して、イテレータを取得し表示する

から要素を取るためにあなたが持っているでしょう

public void remove(Customer customer); 

public void insert(Customer customer, index); 

public void swap(int index1, int index2); 

をすべてのアルゴリズムで:

は、その後、次の()方法、必要ないくつかのメソッドを実装すると

+0

私は、Collection.sort()を使わずにそれをソートしなければならないと思っています。恐ろしい宿題があると考えています。 –

+0

Collection.sort()を使用することはできません。これを試して、やり直しを指示されました。 – Angel918

+0

@ Angel918あなたはレコードをソートするフィールドで言うことができます。私は顧客のIDや名前を使って並べ替えることを意味しますか? –

1

スタートを使用して要素を繰り返しますリストとをリストの他の場所に挿入するか、単にリスト内の要素にスワップします。

+0

私はすでにpublic void insert()を実装していると思います。 – Angel918

0

CustomerクラスのComparatorを作成するか、CustomerクラスのComparableインターフェイスを実装できます。そして、戦略をソート以下のいずれかを使用します。

  1. 、あなたの顧客のためのコンパレータを作成した場合、あなたはComparableインタフェースを実装する場合は、Collections.sort(あなたのリストを使用Collections.sort(リスト、あなたのコンパレータ)
  2. を使用)

次に、表示用にiterator()メソッドを使用します。そしてnext()メソッドを通してリストの次の要素を反復することができます。

関連する問題