0
我的插入方法有问题,因为某些原因导致无限循环。这里是我的结构:C将元素插入到升序链接列表中
struct List {
char element;
struct List *next;
};
这里是我的插入方法:
void insert(struct List *first, char el){
struct List *new=NULL;
struct List *current = first;
new = (struct List*) malloc (sizeof(struct List));
new->element = el;
new->next = NULL;
if (first == NULL){
first = new;
return;
}
while (1){ //this loop never ends
if (current->next == NULL) break;
if (current->next->element < el){
current = current->next;
}else{
break;
}
}
struct List *ex_next = current->next;
current->next = new;
new->next = ex_next;
}
我知道这里类似的问题:C - Inserting into linked list in ascending order但它并没有真正帮助我。
那么,这个循环会重复永远的唯一方法是,如果你有一个循环链接(即'node == node-> next')。 –