0
我试图在链表中插入节点,以便通过dex参数以递增模式对节点进行排序。在有序链接列表中插入
void add(int i){
if(find(i)==NULL){ //if node does not exist
node *m=new node;
m->idx=i;
m->values=NULL;
m->next=NULL;
if(list==NULL){ //if list is empty
list=m;
return;
}
if(i < list->idx){ //if the new node comes before the head node
m->next=list;
list=m;
return;
}
//if the new node is bigger than the maximum node index in the list
if(i > maxIdx(list)){
node *last=lastNode(list);
last->next=m;
}
//else normal insertion
node *prev=list;
node *curr=list->next;
while(curr!=NULL){
if(i < curr->idx){
m->next=curr;
prev->next=m;
return;
}
prev=curr;
curr=curr->next;
}
}
}
使用正确的实现进行编辑,第四个如果之前缺少。
这是功课吗? – 2012-02-11 09:13:47
我是初学者,这不是功课。我能够做头部或尾部插入,现在我正在尝试进行有序插入。 – Vektor88 2012-02-11 09:15:30
'mrow'和'node'之间的关系是什么? – cnicutar 2012-02-11 09:17:05