Iqbal
Iqbal

Reputation: 246

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

            }

        }

    }

Upvotes: 0

Views: 1000

Answers (1)

Beta
Beta

Reputation: 99094

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.

Upvotes: 2

Related Questions