简体   繁体   中英

Compare individual values in two linked lists C Programme

I'm writing a programme that needs to transverse threw 64 linked lists. (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.

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):

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.

   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.

      {
      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'; however, this statement is equivelant to 'x=i; i=i+1;', which does not appear to me helpful.

      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.

      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. if '(temp->val == temp2->val)' [above] evaluates to 'TRUE', the 'continue' statement will cause program flow back to the top of the loop. In addition, the statement 'if(temp->val != temp2->val)' can be omitted due to it will always evaluate to '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. 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;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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