简体   繁体   English

c ++中的循环链表问题

[英]circular linked list problem in c++

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. 4个音符没有问题,但是当我到达5时,已排序的链表似乎挂了。 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.. 我的意思是,例如,第一个数据是一个名字以字母A开头,第二个数字以D开头,第三个数字以C开头..但是当我输入第五个,字母I或K ..我的系统就像挂起的东西..

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";

            }

        }

    }

Take a look at this section: 看看这一节:

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();
  }
}
  1. Yoiu have some untidy logic all through this code. Yoiu通过这段代码有一些不整洁的逻辑。 For instance, checking for `NULL` doesn't make much sense, since there should never be a `NULL` in the circle. 例如,检查“NULL”没有多大意义,因为圆圈中永远不应该有“NULL”。 If you're trying to test the integrity of the list, do it carefully, not like this. 如果您正在尝试测试列表的完整性,请仔细执行,而不是像这样。
  2. This is the only place in the code where an endless loop can occur, so the problem really has to be here. 这是代码中唯一可以发生无限循环的地方,所以问题确实必须在这里。
  3. Think carefully about what it means to sort a circular list, and what happens if you try to add an element that is either before all the others (in alphabetical order), or after all the others. 仔细考虑对循环列表进行排序意味着什么,如果您尝试添加一个元素(在字母顺序之前)或者在所有其他元素之后,会发生什么。 This is where you hang. 这是你挂的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM