나는 LinkedList
을 소트해야하는데 (int
을 포함하고 있습니다.) 어떻게해야하는지 모르겠습니다. 누구든지 int 링크 된 목록을 정렬하려면 내 소스 코드를 줄 수 있습니까?연결된 정수 목록을 정렬 하시겠습니까?
나는이 코드를 온라인에서 찾았지만 작동하지 않았습니다.
public void sort(LinkedList sortlist)
{
//Enter loop only if there are elements in list
boolean swapped = (head != null);
// Only continue loop if a swap is made
while (swapped)
{
swapped = false;
// Maintain pointers
Node curr = head;
Node next = curr.link;
Node prev = null;
// Cannot swap last element with its next
while (next != null)
{
// swap if items in wrong order
if (curr.data>next.data)
{
// notify loop to do one more pass
swapped = true;
// swap elements (swapping head in special case
if (curr == head)
{
head = next;
Node temp = next.link;
next.link = curr;
curr.link = temp;
curr = head;
}
else
{
prev.link = curr.link;
curr.link = next.link;
next.link = curr;
curr = next;
}
}
// move to next element
prev = curr;
curr = curr.link;
next = curr.link;
}
}
}
http://www.cplusplus.com/reference/list/list/sort/이에 대한 U 많이 감사합니다 : 여기
는 STL의 구현을위한 정보입니다 –