类中两个链表的并集中的错误在哪里? [英] Where are the errors in the union of two linked list inside a class ?

查看:60
本文介绍了类中两个链表的并集中的错误在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void uni(list l1,list l2)
 {
  list u;
  node *t1,*t2;
  t1=l1.head;
  t2=l2.head;
  while(t1!=NULL)
   {
    u.push(t1->prn);
    t1=t1->next;
   }
  while(t2!=NULL)
   {
    t1=l1.head;
    while(t1!=NULL)
    {
     if(t2->prn==t1->prn)
     {
      u.push(t2->prn);
      t1=t1->next;
      break;
     }
      t1=t1->next;
    }
    t2=t2->next;
   }
  u.display();
 }





我的尝试:



我在类中尝试了两个链表的并集!



What I have tried:

I've tried the union of two linked lists in a class!

推荐答案

这是编码器调试他/她的代码的任务。使用调试器。做一些输出。



我想你检查第二个列表中的某个节点是否已经是新列表的成员出错了。编写一个find函数并用一些明显的数据进行测试。
This is the task you the coder to debug his/her code. Use the debugger. Make some output.

I guess that your check if some node of the second list is already member of the new list goes wrong. Write a find function and test it with some obvious data.


void uni(list l1, list l2)
{
  list u;
  node *t1,*t2;
  t1=l1.head;
  t2=l2.head;
  while(t1!=NULL)
  {
    u.push(t1->prn);
    t1=t1->next;
  }
  while(t2!=NULL)
  {
    t1=l1.head;
    while(t1!=NULL)
    {
      if(t2->prn==t1->prn)
        break; // exit the loop as soon as there is a match
      t1=t1->next;
    }
    if (t1==NULL)
      u.push(t2->prn); // no match, we should add the item
    t2=t2->next;
  }
  u.display();
}


这篇关于类中两个链表的并集中的错误在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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