为什么我的链表代码不起作用? 4个音符没有问题,但是当我到达5时,已排序的链表似乎挂了。它不知道在哪里追加并放置新节点吗?
我的意思是,例如,第一个数据是一个名字以字母A开头,第二个数字以D开头,第三个以C开头..但是当我输入第五个,字母I或K ..我的系统就像挂什么东西..
Node * next;
Node * prev;
userData * data;
Node * start;
Node * end;
void addUserData(userData * data){
string name = data->getName();
Node* node = new Node(data);
if(isEmpty()){
start = node;
end = node;
start->Next(end);
end->Prev(start);
return;
}
else if(!isEmpty() && start == end){
node->Next(start);
node->Prev(start);
start->Next(node);
start->Prev(node);
end = node;
if(start->getUserData()->getName().compare(node->getUserData()->getName())>0){
end = start;
start = node;
}
return;
}
else{
Node *temp = start;
if(name.compare("N") < 0){
while(temp->getNext()->getNext()!=NULL && name.compare(temp->getNext()->getUserData()->getName())>0){
temp = temp ->getNext();
}
}
else{
temp = end;
while(name.compare(temp->getUserData()->getName())<0){
temp = temp ->getPrev();
}
}
if(name.compare(temp->getUserData()->getName())>0){
node ->Next(temp->getNext());
node ->Prev(temp);
temp->Next(node);
node->getNext()->Prev(node);
if(name.compare(end->getUserData()->getName())>0){
end = node;
}
}else if(name.compare(temp->getUserData()->getName())<0){
node->Next(temp);
node->Prev(temp->getPrev());
temp->getPrev()->Next(node);
temp->Prev(node);
if(name.compare(start->getUserData()->getName())<0){
start = node;
}
}else{
cout<<"Name already exist\n";
}
}
}
答案 0 :(得分:2)
看看这一节:
if(name.compare("N") < 0){
while(temp->getNext()->getNext()!=NULL && name.compare(temp->getNext()->getUserData()->getName())>0){
temp = temp ->getNext();
}
}
else{
temp = end;
while(name.compare(temp->getUserData()->getName())<0){
temp = temp ->getPrev();
}
}