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