c++ 中的循环链表问题

发布于 2024-11-02 07:59:51 字数 2463 浏览 7 评论 0原文

为什么我的链表代码不起作用? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-11-09 07:59:51

看看这一部分:

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 这段代码中有一些不整洁的逻辑。例如,检查“NULL”没有多大意义,因为圆圈中不应该有“NULL”。如果您想测试列表的完整性,请小心行事,而不是像这样。
  2. 这是代码中唯一可能发生无限循环的地方,所以问题确实就在这里。
  3. 仔细考虑对循环列表进行排序意味着什么,以及如果您尝试添加一个位于所有其他元素之前(按字母顺序)或之后的元素,会发生什么情况。这就是你挂的地方。

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. For instance, checking for `NULL` doesn't make much sense, since there should never be a `NULL` in the circle. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文