C ++ Linked List Printing ...我得到了无限循环 [英] C++ Linked List Printing... I get the infinite loop

查看:147
本文介绍了C ++ Linked List Printing ...我得到了无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的主要函数中使用了这个函数但是不工作

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



我希望打印整个列表从...获得无限循环。

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

即使我简化它只是打印列表中的前两个元素
我不会得到无限循环,但对于不同的两个节点仍然得到相同的东西



例如,我的第一个节点是A1,第二个是A2,
我希望得到A1 ----> A2,但我得到的是A1 ----> A1 ---->



我想我

 <$ c 

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

if(header-> next == tail)
{
节点* n = new Nodes;
n-> elem = e;
n-> next = tail;
n-> prev = tail-> prev;
tail-> prev-> next = tail-> prev = n;
header-> next = n; // 很重要!!!!
}
else
{
if(d == tail)
{
节点* n =新节点;
n-> elem = e;
n-> next = tail;
n-> prev = tail-> prev;
tail-> prev = n;
}
else
{
节点* n =新节点;
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. d == head(需要更改为newNode)

  2. head == tail(需要更改头和尾,这也是列表为空时为head == tail == NULL的情况)

您也可以考虑拜访您的助教或您的教师办公时间。



您对非常基础的概念有很多疑问,如果你没有从你的演讲笔记收集足够的信息,以理解这些想法(从所有的帖子)背后的逻辑,那么你应该试着联系你的教师,TA或其他选择在校园里的帮助。因为这些都是非常重要的想法,以完全理解未来的定制数据结构和应用程序开发。


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); } 

解决方案

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.

这篇关于C ++ Linked List Printing ...我得到了无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆