단일 원형 연결리스트에서 정렬방법
void SortByHp_SL(List *plist)
{
int hp = 0;
int num = plist->num;
int num2 = 0;
plist->cur = plist->head->next;
plist->sort = plist->head->next;
while (num > 0)
{
num2 = num;
while (num2 > 0)
{
if (hp > plist->cur->hp)
{
hp = plist->cur->hp;
plist->maxHp = plist->cur;
}
plist->cur->next;
num2--;
}
Swap(plist->maxHp, plist->sort);
plist->sort = plist->sort->next;
plist->cur = plist->cur->next;
num--;
}
}
void Swap(Node *first,Node *second)
{
struct champion temp = { 0, };
//maxHp의 데이터를 temp에 임시로 옮긴다.
temp.hp = first->hp;
temp.mp = first->mp;
temp.speed = first->speed;
temp.range = first->range;
strcpy(temp.name, first->name);
strcpy(temp.position, first->position);
//first의 데이터를 초기화한다.
first->hp = 0;
first->mp = 0;
first->speed = 0;
first->range = 0;
memset(first->name, 0, sizeof(first->name));
memset(first->position, 0, sizeof(first->position));
//second의 데이터를 first로 옮긴다.
first->hp = second->hp;
first->mp = second->mp;
first->range = second->range;
first->speed = second->speed;
strcpy(first->name, second->name);
strcpy(first->position, second->position);
//second의 데이터를 초기화한다.
second->hp = 0;
second->mp = 0;
second->speed = 0;
second->range = 0;
memset(second->name, 0, sizeof(second->name));
memset(second->position, 0, sizeof(second->position));
//temp의 데이터를 second로 옮긴다.
second->hp = temp.hp;
second->mp = temp.mp;
second->speed = temp.speed;
second->range = temp.range;
strcpy(second->name, temp.name);
strcpy(second->position, temp.position);
}
정렬이 안됩니다 뭐가 잘못된 걸까요?
참고로 Head 노드는 맨 앞이 아니라 맨 뒤에 있습니다.
list.h 는 이중 연결 리스트의 간단한 구현이고,
list.h 는 이중 연결 리스트의 간단한 구현이고,
champ1.c 는 리스트의 링크를 바꾸고,
champ2.c 는 새로운 헤드에 링크 하고,
champ3.c 는 (시도하신 것 처럼) 원소의 값을 바꿉니다.
댓글 달기