C#의 일반 목록으로 작업하고 있지만 버블 정렬 방법을 사용하여 노드를 정렬하려고 할 때 문제가 있습니다.C#의 일반 목록에있는 Bubblesort
namespace ConsoleApplication1
{
public class GenericList
{
private class Node
{
// Each node has a reference to the next node in the list.
public Node Next;
public int Data;
}
// The list is initially empty.
private Node head = null;
// Add a node at the beginning of the list with t as its data value.
public void AddNode(int t)
{
Node newNode = new Node();
newNode.Next = head;
newNode.Data = t;
head = newNode;
}
//list length
public int Size()
{
int listsize= 0;
Node current = head;
while (current != null)
{
listsize++;
current = current.Next;
}
return listsize;
}
//bubble sort
public void bubblesort()
{
int size = Size();
Node current = head;
for (int i = 1; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (current.Data > current.Next.Data && current.Next!=null)
{
int temp = current.Data;
current.Data = current.Next.Data;
current.Next.Data = temp;
}
}
}
head = current;
}
}
}
목록에 5 개 이상의 노드를 추가하면 bubblesort 메서드가 작동을 멈추고 (올바르게 정렬되지 않습니다). 아무도 도와 줄 수 있습니까?
이 숙제입니까? 그렇지 않은 경우에는 List.Sort 메소드를 사용하십시오. – tvanfosson
물론 그 코멘트와 함께 대학 수업 임무 같아요 ... 우. –
일반 목록을 호출하면 사용자 정의'GenericList '이 아닌'System.Collections.Generic.List '을 사용하는 것처럼 들릴 것입니다. 이것이 숙제가 아니라면'GenericList'를 없애고'List '(또는'LinkedList ')을 사용하십시오. –
Justin