c++ 中的循环链表问题
为什么我的链表代码不起作用? 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";
}
}
}
Why doesn't my linked list code work? It has no problem with 4 notes, but when I get to 5 the sorted linked list seems to hang. Does it not know where to append and place the new node?
I mean,for example the first data is a name starts with letter A, and the second start with D and the 3rd start with C.. but when i enter for the 5th, letter I or K.. my system like hang or something..
Node
Node * next;
Node * prev;
userData * data;
List
Node * start;
Node * end;
Function that ads a new node in alphabetical order
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";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这一部分:
Take a look at this section: