0
링크 된 목록의 정렬 된 삽입에 대해 하나의 반복 함수를 구현하는 데 어려움이 있습니다. 나는 이전에 insert()
함수가 호출 될 때 훨씬 더 쉽게 재귀와 그 짓을하지만, 여기에 내가 (l->data < data)
조건을 구현하는 방법에 조금 혼란 스러워요 :링크 된 목록을 사용하여 정렬 된 삽입
typedef struct E_Type * List;
struct E_Type
{
int data;
struct E_Type* next;
};
및 기능 :
insert(List & l, int data){
if (l == 0 || l->data > data){
List new_list = new E_Type;
new_list->data = data;
new_list->next = l;
l = new_list;
}
else if (l->data < data){
List new_list = new E_Type;
new_list->data = data;
new_list->next = l; //i am shooting in the dark with this one
l = new_list;
}
}
또는 현명하게 구현하고 더미 헤드를 사용하면 코드를 크게 단순화 할 수 있습니다. – Voo
감사합니다. 이제이 제안을 구현하려고합니다. – EmilDo