2012-01-05 8 views
1

の実行を停止しません。ここでの問題は、実行時にそれだけで稼働し続け、停止しないと、問題がどこにあるか私はわからないんです方法バブルソートは

public void sortStudentsAlphabeticallyByFirstName() 
{ 
    StudentNode unsorted = tail; 
    StudentNode current = header; 
    while(current != null) 
    { 
     while(current != unsorted) 
     { 
      int result = (current.nextNode().getFirstName()).compareToIgnoreCase(current.getFirstName()); 
      if(result < 0) 
      { 
       StudentNode temp = current; 
       current = current.nextNode(); 
       current.setNext(temp); 
      } 
     } 
     current = current.nextNode(); 
     unsorted = unsorted.prevNode(); 
    } 
} 

です。

+3

まあ、ループしながら、あなたの内を考えると、それはfalseを返すことはありませんかもしれない状況がありますかどうかを確認します。それはかなり明白です - あなたはそれを見なければなりません。一般的には、このような問題では、デバッグ出力はあなたの親友です。内側のwhileループ内に現在の要素を表示すると、何が間違っているかを素早く知ることができます。これは宿題のように見えるので、そのままにしておきます。 – EboMike

+0

使用するデータタイプを確認できるようにコードを完成できますか? –

+0

だから、家事ええ? – Viele

答えて

1

私たちのリンクリストは、C、BとDのノードを持って考えてみましょう。ループ

current = C; 

はので、このコードを使用している間、あなたの第二に入力すると言う:

temp = current; // i.e. temp = C as current = C 
current = current.next(); // say current = B now and temp = C 
current.setNext(temp); // here B's next is set to C 
         // but you forgot A's next is C in the example, now since B 
         // is taking it's place so A's next must point to B 
         // B's next must point to C and C's next must point to D. 

ですから、これらの手順を、忘れてしまったように思えるあなたがした後、次のノードに現在動いている

それは、温度と電流が交換されます。しかし、例のtemp Aの前のものはBを指していなければならず、これはCとスワップされています.BがDを前に指していたので、今度はCがDを指していなければなりません。 Cは(それはあなたが3行目にやったことだ。)

EDIT 全体の作業コードは、より多くの情報のために追加されました。

import java.io.*; 

class Node 
{ 
public Node previous; 
public String value; 
public Node next; 
} 

public class LinkedList 
{ 
private BufferedReader br ; 
private String str; 
private int totalNodes; 

private Node current, previous, temp, head, tail; 

public LinkedList() 
{ 
    br = new BufferedReader(new InputStreamReader(System.in)); 
    current = previous = temp = head = tail = null; 
    totalNodes = 0; 
} 

public static void main(String[] args) 
{ 
    LinkedList ll = new LinkedList(); 
    ll.menu(); 
} 

private void menu() 
{ 
    boolean flag = true; 
    int choice = 0; 
    while(flag) 
    { 
     System.out.println("--------------------------------------------------"); 
     System.out.println("---------------------MENU-----------------------"); 
     System.out.println("Press 1 : To ADD Node at the END."); 
     System.out.println("Press 2 : To ADD Node at the BEGINNING."); 
     System.out.println("Press 3 : To Add Node in BETWEEN the List."); 
     System.out.println("Press 4 : To SORT the List"); 
     System.out.println("Press 5 : To DISPLAY the List."); 
     System.out.println("Press 6 : To EXIT the Program."); 
     System.out.println("--------------------------------------------------"); 
     System.out.print("Please Enter your choice here : "); 
     try 
     { 
      str = br.readLine(); 
      choice = Integer.parseInt(str); 
      if (choice == 6) 
      { 
       flag = false; 
      } 
      accept(choice); 
     } 
     catch(NumberFormatException nfe) 
     { 
      System.out.println("OUCH!, Number Format Exception, entotalNodesered."); 
      nfe.printStackTrace(); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("OUCH!, IOException, entotalNodesered."); 
      ioe.printStackTrace(); 

     } 
    } 
} 

private void accept(int choice) 
{ 
    switch(choice) 
    { 
     case 1: 
      addNodeToListAtStart(); 
      break; 
     case 4: 
      sortListBubble(); 
      break; 
     case 5: 
      displayList(); 
      break; 
     case 6: 
      System.out.println("Program is Exiting."); 
      break; 
     default: 
      System.out.println("Invalid Choice.\nPlease Refer Menu for further Assistance."); 
    } 
} 

private void addNodeToListAtStart() 
{ 
    if (head != null) 
    { 
     current = new Node(); 
     System.out.print("Enter value for the New Node : "); 
     try 
     { 
      str = br.readLine(); 
     } 
     catch(NumberFormatException nfe) 
     { 
      System.out.println("OUCH!, Number Format Exception, entotalNodesered."); 
      nfe.printStackTrace(); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("OUCH!, IOException, entotalNodesered."); 
      ioe.printStackTrace();    
     } 
     current.previous = tail; 
     current.value = str; 
     current.next = null; 
     tail.next = current; 
     tail = current; 
    } 
    else if (head == null) 
    { 
     current = new Node(); 
     System.out.print("Enter value for the New Node : "); 
     try 
     { 
      str = br.readLine(); 
     } 
     catch(NumberFormatException nfe) 
     { 
      System.out.println("OUCH!, Number Format Exception, entotalNodesered."); 
      nfe.printStackTrace(); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("OUCH!, IOException, entotalNodesered."); 
      ioe.printStackTrace();    
     } 
     current.previous = null; 
     current.value = str; 
     current.next = null;    
     head = current; 
     tail = current; 
    } 
    totalNodes++; 
} 

private void displayList() 
{ 
    current = head; 
    System.out.println("----------DISPLAYING THE CONTENTS OF THE LINKED LIST---------"); 
    while (current != null) 
    { 
     System.out.println("******************************************"); 
     System.out.println("Node ADDRESS is : " + current); 
     System.out.println("PREVIOUS Node is at : " + current.previous); 
     System.out.println("VALUE in the Node is : " + current.value); 
     System.out.println("NEXT Node is at : " + current.next); 
     System.out.println("******************************************"); 
     current = current.next; 
    } 
} 

private boolean sortListBubble() 
{ 
    // For Example Say our List is 5, 3, 1, 2, 4 
    Node node1 = null, node2 = null; // These will act as reference. for the loop to continue 
    temp = head; // temp is set to the first node. 

    if (temp == tail || temp == null) 
     return false; 

    current = temp.next; // current has been set to second node. 

    for (int i = 0; i < totalNodes; i++) // this loop will run till whole list is not sorted. 
    { 
     temp = head; // temp will point to the first element of the list. 
     while (temp != tail) // till temp won't reach the second last, as it reaches the last element loop will stop. 
     { 
      if (temp != null) 
       current = temp.next; 
      while (current != null) // till current is not null. 
      { 
       int result = (temp.value).compareToIgnoreCase(current.value); 
       if (result > 0) // if elment on right side is higher in value then swap. 
       { 
        if (temp != head && current != tail) // if nodes are between the list. 
        { 
         current.previous = temp.previous; 
         (temp.previous).next = current; 
         temp.next = current.next; 
         (current.next).previous = temp;      
         current.next = temp; 
         temp.previous = current; 
        } 
        else if (current == tail) // if nodes to be swapped are second last and last(current) 
        { 
         temp.next = current.next; 
         current.previous = temp.previous; 
         if (temp.previous != null) 
          (temp.previous).next = current; 
         else 
          head = current; 
         temp.previous = current; 
         current.next = temp; 
         tail = temp; 
        } 
        else if (temp == head) // if the first two nodes are being swapped. 
        { 
         temp.next = current.next;      
         (current.next).previous = temp; 
         current.previous = temp.previous; 
         temp.previous = current; 
         current.next = temp; 
         head = current; 
        } 
        current = temp.next; // since swapping took place, current went to the left of temp, that's why 
                // again to bring it on the right side of temp. 
       } 
       else if (result <= 0) // if no swapping is to take place, then this thing 
       { 
        temp = current; // temp will move one place forward 
        current = current.next; // current will move one place forward 
       }          
      } 
      if (temp != null) 
       temp = temp.next; 
      else // if temp reaches the tail, so it will be null, hence changing it manually to tail to break the loop. 
       temp = tail; 
     } 
    } 
    return true; 
} 
} 

うまくいけば助けてください。

よろしく

+0

O.k 'StudentNode temp = current;に変更しました。 StudentNode next = current.nextNode(); StudentNode previous = current.prevNode(); StudentNode nextNext = next.nextNode(); current = current.nextNode(); current.setNext(temp); temp.setNext(nextNext); nextNext.setPrev(temp); previous.setNext(現在); temp.setPrev(現在);現在は です。setPrev(前); ' –

+0

しかし、まだ実行し続けています(フォーマットには申し訳ありません) –

+0

@DaveShaw:あなたが何をやっているのか、私はもう少し答えを編集しています。よろしく –

1

私はバブルソートアルゴリズムを見て以来、長い間あったが、以下の部分が間違っているようだ

StudentNode temp = current; 
current = current.nextNode(); 
current.setNext(temp); 

あなたは、ノードAで始まる言っています - > B - > C(A =現在) 。あなたはcurrent = B(ライン2)、current.next = Aで終了しますが、あなたがのnextを交換したことがないので、current.next.nextは、再び現在であなたのtemp変数

関連する問題