C++ Linked List Printing… I get the infinite loop

穿精又带淫゛_ 提交于 2020-01-16 19:07:09

问题


I used this in my main function but it's not working

void LinkedList::TraPrinHead(const LinkedList& p)
{
  Nodes* currentNodes = header->next;
  while( currentNodes != tail ) {
     cout << currentNodes->elem << " ----> ";
     currentNodes = currentNodes->next; }
}

I expect to print the whole list from this... but I keep getting the infinite loop.

   cout << currentNodes->elem << " ----> ";
   currentNodes = currentNodes->next;
   cout << currentNodes->elem << " ----> ";
   currentNodes = currentNodes->next;

And even if I simplify it just to print out the first two elements on the list I do not get the infinite loop but keep getting the same thing for the different two nodes

For example, my first node was A1, second was A2, but with that function I expect to get A1 ----> A2 but what I get is A1 ----> A1 ---->

I think I have problems on my add function.

this is the function that I use

  void LinkedList::InsertDoublyBefore(Nodes* d, const string& e) {

  if (header->next == tail) 
  { 
     Nodes* n = new Nodes;
     n->elem = e; 
     n->next = tail;
     n->prev = tail->prev;
     tail->prev->next = tail->prev = n; 
     header->next = n; // very important!!!!
  }
  else
  {
       if (d==tail) 
        {
         Nodes* n = new Nodes;
         n->elem = e;
         n->next = tail;
         n->prev = tail->prev;
         tail->prev = n;
         }
       else
       {
         Nodes* n = new Nodes; 
         n->elem = e; 
         n->next = d; 
         n->prev = d->prev;
         d->prev->next = d->prev = n; 
        }
      }

     }

     void LinkedList::InsertDoublyAfter(Nodes* d, const string& e) 
     {
         InsertDoublyBefore(d->next, e);
     }

   void LinkedList::addtoFront(const string& e)  { InsertDoublyBefore(header->next, e); }
   void LinkedList::addtoBack(const string& e) { InsertDoublyBefore(tail, e); } 

回答1:


Your cases are a little redundant. The cases that should be handled by this specific insert function include.

  1. d==head (need to change head to newNode)
  2. head==tail (need to change head and tail, this is also case when list is empty as head==tail==NULL)

you also may want to consider visiting your TA or your instructors office hours.

You have a lot of questions about very foundational concepts, and if you aren't gathering enough information from your lecture notes to comprehend the logic behind these ideas(from all of your posts) then you should be trying to reach out to your instructor, TA, or other options for assistance on campus. As these are very important ideas to fully understand for the future development of customized data structures and application development.




回答2:


This line

    tail->prev->next = tail->prev = n;

doesn't look right. After tail->prev = n you're going to modify n->next with tail->prev->next = ... and not really tail->prev->next. Actually, you have undefined behavior here because you modify and then use the same variable (tail->prev) in the expression and that's much worse.

Here

   if (d==tail) 
    {
     Nodes* n = new Nodes;
     n->elem = e;
     n->next = tail;
     n->prev = tail->prev;
     tail->prev = n;
     }

you seem to be modifying only half of the links.

And here

   {
     Nodes* n = new Nodes; 
     n->elem = e; 
     n->next = d; 
     n->prev = d->prev;
     d->prev->next = d->prev = n; 
    }

you have problems similar to the aforementioned ones.

Use your debugger. But before that, work out everything on paper.



来源:https://stackoverflow.com/questions/15994609/c-linked-list-printing-i-get-the-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!