21921

How to get the common elements of the two sets in a Linked List?

I have got 2 linkedlist, I want to compare both lists and print every common element that appears in both list. tried everything can't get it to work.

struct Node *calcIntersection(struct Node *headA, struct Node *headB) { struct Node * link1 = headA; struct Node * link2 = headB; while(link1 != NULL) { if (link2->value == link2->value) { printf("%d", link1->value); } link1 = link1 -> pNext; } return link1; }

Answer1:

You are close, in reality your method should look like this:

struct Node *calcIntersection(struct Node *headA, struct Node *headB) { struct Node *link1 = headA; while(link1 != NULL) { struct Node *link2 = headB; while (link2 != NULL) { if (link1->value == link2->value) { printf("%d", link1->value); } link2 = link2->pNext; } link1 = link1->pNext; } return link1; }

I'm not quite sure what you are returning there, but in most situations you will be returning NULL, I'm not quite sure if that's what you want however.

Answer2:

OK.. This looks like homework.. so I will point out a few things that are pretty obvious in the code above

1) The condition you are checking for (link2->value == link2->value) is probably a typo. You might have meant link2->value == link1->value. 2) You are iterating over every element in list 1 but NOT in list 2. So you are checking only if the first element in list 2 is found in list 1.

I think this is sufficient.. for now.. :)

Answer3:

if (link2->value == link2->value)

Should be

if (link1->value == link2->value)

As pointed by gbulmer, you should also iterate over the second list and compare it to the first one.

Recommend

  • Marshalling a VARIANT back from COM to C++ client
  • Align two inline-block div with content
  • Parse between comments in Simple HTML Dom
  • Grant security to a private key in Windows Server 2012 via powershell w/out external DLLs, etc
  • get the text in a list item and modify it with javascript
  • Get the next href in a list in another file with jQuery?
  • Unable to link my own static libraries
  • Set up the Android-Facebook sdk for Netbeans
  • how to resolve OAuthException: (#100)
  • Why cout is producing no output on Code Blocks?
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Delete MySQLi record without showing the id in the URL
  • Apache 2.4 - remove | delete | uninstall
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?