比较两个链表C程序中的单个值

比较两个链表C程序中的单个值,c,struct,linked-list,C,Struct,Linked List,我正在写一个程序,需要创建64个链表。(每个节点有一个名为val的整数变量)它需要比较每个节点,如果val等于任何列表的任何其他节点中的另一个val,则必须记录它 我编写了一个函数,该函数会抛出列表,但当它打印出与列表相等的结果后,就会崩溃,我的函数如下所示(n=64): void扫描列表(int n) { int i=0; int j=0; 对于(i=0;ival==temp2->val) { printf(“它们在同一行,%d=%d\n”,temp->val,temp2->val); tem

我正在写一个程序,需要创建64个链表。(每个节点有一个名为val的整数变量)它需要比较每个节点,如果val等于任何列表的任何其他节点中的另一个val,则必须记录它

我编写了一个函数,该函数会抛出列表,但当它打印出与列表相等的结果后,就会崩溃,我的函数如下所示(n=64):

void扫描列表(int n)
{
int i=0;
int j=0;
对于(i=0;ival==temp2->val)
{
printf(“它们在同一行,%d=%d\n”,temp->val,temp2->val);
temp2=temp2->next;
继续;
}
否则如果(临时->瓦尔!=临时2->瓦尔)
{
temp2=temp2->next;
继续;
}
else if(temp2==NULL)
{
温度=温度->下一步;
temp2=头[x];
继续;
}
}
}
}

我的链表代码如下所示:

struct node
 {
  int val;
  struct node *next;
} ;
 struct node* heads[64]; // array with the roots of the lists
 struct node* currs[64]; // array holding pointers to current positions in list

 struct node* create_list(int val, int listNo){
     struct node *ptr = (struct node*)malloc(sizeof(struct node));

     ptr->val = val;
     ptr->next = NULL;

     heads[listNo] = currs[listNo] = ptr;
     return ptr;
 }
void setup_list_array(int n){
    int i;

    for(i=0; i<n; i++)
        {
            heads[i] = NULL;
            currs[i] = heads[i];
        }
 }
struct节点
{
int-val;
结构节点*下一步;
} ;
结构节点*头[64];//具有列表根的数组
结构节点*currs[64];//包含指向列表中当前位置的指针的数组
结构节点*创建列表(int val,int listNo){
结构节点*ptr=(结构节点*)malloc(sizeof(结构节点));
ptr->val=val;
ptr->next=NULL;
heads[listNo]=currs[listNo]=ptr;
返回ptr;
}
无效设置列表数组(int n){
int i;

对于(i=0;i首先,对“问题”代码进行一些小评论:

void scanlist(int n)
   {
   int i = 0;
   int j = 0;
“j”似乎未使用

   for(i=0; i < n; i++)
为了将“x”初始化为“i+1”,可能会增加“i”;但是,此语句等价于“x=i;i=i+1;”,这对我来说似乎没有帮助

      struct node *temp2;  //Declare temp2
      temp2 = heads[x];       //Assign Starting Address to temp2
由于前面提到的“x”的错误初始化,“temp”和“temp2”现在指向同一个“head[]”元素

      while(temp != NULL)
         {
         if(temp->val == temp2->val)
            {
            printf("theyre on the same line, %d = %d  \n", temp->val, temp2->val);
            temp2 = temp2->next;
            continue;
            }
         else if(temp->val != temp2->val)
此处可以省略“else”语句。如果“(temp->val==temp2->val)”的计算结果为“TRUE”,则“continue”语句将导致程序流返回循环顶部。 此外,语句“if(temp->val!=temp2->val)”可以省略,因为它的计算结果总是“TRUE”

            {
            temp2 = temp2->next;
            continue;
            }
         else if(temp2 == NULL)
由于此'else'语句,如果上述任一'if'语句的计算结果为'TRUE',则不会执行此代码。这似乎是一个缺陷

            {
            temp = temp->next;
            temp2 = heads[x];
            continue;
            }
         }
      }
   }
下面是实现此方法的另一种方法(包括注释描述正在发生的事情)

void扫描列表(
int n
)
{
int i;
/*迭代列表标题*/
对于(i=0;i<(n-1);+i)
{
struct node*temp=heads[i];//声明temp并分配起始地址
/*迭代主列表节点*/
while(临时)
{
int j;
/*迭代列表头以进行比较*/
对于(j=i+1;jval==temp2->val)
printf(“它们在同一行,%d=%d\n”,temp->val,temp2->val);
temp2=temp2->next;
}
}
}
温度=温度->下一步;
}
返回;
}

您是否注意到每次跳过一个列表?您是如何向列表中添加元素的?我每次都试图将第n个列表与第n+1个列表进行比较?我使用此函数向列表中添加:struct node*add_to_list_in_array(int val,int listNo){if(NULL==heads[listNo]){return(create_list(val,listNo))}结构节点ptr=(结构节点)malloc(sizeof(结构节点));ptr->val=val;ptr->next=NULL;currs[listNo]->next=ptr;currs[listNo]=ptr;return ptr;}
int x=i++;
您是否意识到
i
因此而增加了两倍?因此您正在比较0和1、2和3等。缺少1和2..可能seg故障只是由于这一原因
      while(temp != NULL)
         {
         if(temp->val == temp2->val)
            {
            printf("theyre on the same line, %d = %d  \n", temp->val, temp2->val);
            temp2 = temp2->next;
            continue;
            }
         else if(temp->val != temp2->val)
            {
            temp2 = temp2->next;
            continue;
            }
         else if(temp2 == NULL)
            {
            temp = temp->next;
            temp2 = heads[x];
            continue;
            }
         }
      }
   }
void scanlist(
      int n
      )
   {
   int i;

   /* Iterate the list heads. */
   for(i=0; i < (n-1); ++i)
      {
      struct node *temp = heads[i];   // Declare temp and assign Starting Address

      /* Iterate primary list nodes. */
      while(temp)
         {
         int j;

         /* Iterate list heads to compare */
         for(j=i+1; j < n; ++j)
            {
            struct node *temp2 = heads[j];  //Declare temp2 and assign Starting Address

            /* Iterate list nodes to compare */
            while(temp2)
               {
               if(temp->val == temp2->val)
                  printf("theyre on the same line, %d = %d  \n", temp->val, temp2->val);

               temp2=temp2->next;
               }
            }
         }

      temp=temp->next;
      }

   return;
   }