简体   繁体   English

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

[英]Compare individual values in two linked lists C Programme

I'm writing a programme that needs to transverse threw 64 linked lists. 我正在编写一个需要横向抛出64个链表的程序。 (each node having one integer variable named val) it needs to compare each node and if the val is equal to another val in any other node of any list it must record it. (每个节点都有一个名为val的整数变量),它需要比较每个节点,如果val等于任何列表中任何其他节点中的另一个val,则必须记录它。

i've written a function that transverse threw the lists but after it prints the results that they equal it crashes, my function looks like this (n = 64): 我编写了一个将列表横向抛出的函数,但是在打印出等于它们崩溃的结果之后,我的函数看起来像这样(n = 64):

void scanlist(int n)
{

int i = 0;
int j = 0;
    for(i=0; i < n; i++)
        {
            struct node *temp;  //Declare temp
             temp = heads[i];       //Assign Starting Address to temp

              int x = i++;
                     struct node *temp2;  //Declare temp2
                        temp2 = heads[x];       //Assign Starting Address to temp2


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






                    }


    }

} }

my linked list code looks like this: 我的链表代码如下所示:

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

thanks for any help in advance, hope i was clear. 感谢您的任何事先帮助,希望我很清楚。

First, a few small comments on the 'Question' code: 首先,对“问题”代码进行一些评论:

void scanlist(int n)
   {
   int i = 0;
   int j = 0;

It appears that 'j' is unused. 看来“ j”未使用。

   for(i=0; i < n; i++)

Perhaps it would be more efficent to this to 也许这样做会更有效

for(i=0; i < (n-1); i++)  

This will avoid referencing the last 'heads[]', due to it being already compared. 由于已经比较过,这将避免引用最后的“ heads []”。

      {
      struct node *temp;  //Declare temp
      temp = heads[i];       //Assign Starting Address to temp

      int x = i++;

Perhaps 'i' is incremented in order to initialize 'x' to 'i + 1'; 也许增加“ i”以将“ x”初始化为“ i + 1”; however, this statement is equivelant to 'x=i; 但是,该语句等同于'x = i; i=i+1;', which does not appear to me helpful. i = i + 1;',这对我似乎没有帮助。

      struct node *temp2;  //Declare temp2
      temp2 = heads[x];       //Assign Starting Address to temp2

Due to the previously stated mis-initialization of 'x', 'temp' and 'temp2' now point to the same 'head[]' element. 由于先前声明的对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)

The 'else' statement can be omitted here. 在此可以省略“ else”语句。 if '(temp->val == temp2->val)' [above] evaluates to 'TRUE', the 'continue' statement will cause program flow back to the top of the loop. 如果'(temp-> val == temp2-> val)'[以上]计算为'TRUE',则'continue'语句将导致程序流回到循环的顶部。 In addition, the statement 'if(temp->val != temp2->val)' can be omitted due to it will always evaluate to 'TRUE' 此外,可以省略语句“ if(temp-> val!= temp2-> val)”,因为该语句始终为“ TRUE”

            {
            temp2 = temp2->next;
            continue;
            }
         else if(temp2 == NULL)

Due to this 'else' statement, if either of the above 'if' statements evaluate to 'TRUE', this code will not be executed. 由于此“ else”语句,如果以上两个“ if”语句中的任何一个评估为“ TRUE”,该代码均不会执行。 This appears to be a flaw. 这似乎是一个缺陷。

            {
            temp = temp->next;
            temp2 = heads[x];
            continue;
            }
         }
      }
   }

Below, another way to implement this method (included comments describe what is going on). 下面是实现此方法的另一种方法(包含的注释描述了正在发生的事情)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM