0
ノードがde idxパラメータによって昇順に並べ替えられるように、リンクリストにノードを挿入しようとしています。順序付きリンクリストへの挿入
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;
}
}
}
正しい実装で編集された場合、4番目のifは以前には欠落していました。
この宿題はありますか? –
私は初心者ですが、これは宿題ではありません。私は頭や尾の挿入をすることができます、そして今私は注文挿入をしようとしています。 – Vektor88
'mrow'と' node'の関係は何ですか? – cnicutar