Not Yet Answered # How do i know if two linked lists are set equal?

LevyDee 2 Buffalo101 -2 pratik_garg -5 L7Sqr 227 L7Sqr 227 pratik_garg -5 L7Sqr 227 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

If you want the ability to compare even if they are not in the same order, but contain the same sets of data, when you overload your == operator you could temporarily sort the list and than compare.

0

You traverse the first list. Let's say you're at element 0.

You check your second list until you find an element = element 0 from the first list. Two elements (nodes) are equal if all their fields are equal.

If you don't find an element from list2 = element 0 from list 1, the lists are not equal.

If you do find an element from list 2 = element 0 from list 1, you start the algorithm again for element 1 from list 1.

*Edited 5 Years Ago by Buffalo101*: n/a

0

good logic @Buffalo101.

But you have forget one thing to compare node number in both the list.

@rockerjhr please add this line in above algorithm in first position

if number_of_first_list is not equal to number_of_second_list

then return (false) // means lists are not same..

else

/*

that code for checking node by node values..

*/

0

But you have forget one thing to compare node number in both the list.

It really depends on what a 'set' constitutes here. Remember, in its truest form, a set has no duplicates so converting an array (or list) to a set can change its size. For instance

```
List: [a,b,b,c,d,a,a,a]
Converts to
Set: [a,b,c,d]
```

So the length of the first two lists is not necessarily an indicator of a failure condition.

*Edited 5 Years Ago by L7Sqr*: n/a

0

How do i know if two linked lists are set equal?

There are a couple of suggestions above. If there is no need to worry about optimal runtime performance then they are probably just as easy to implement.

One way to do this in better than `O(n*n)`

is to convert the lists to sorted arrays (possibly removing duplicates) and iterate the two resulting lists together comparing that they are equal.

In pseudocode, that looks something like:

```
L1 : 1 - 7 - 4 - 3 - 1
L2 : 4 - 1 - 7 - 4 - 3
sort_list L1 as S1 -> [1,3,4,7] # duplicates removed
sort_list L2 as S2 -> [1,3,4,7] # duplicates removed
return false unless length(S1) == length(S2)
while length(S1) > 0
a = S1.pop
b = S2.pop
return false unless a == b
return true
```

0

yes it would run for above example....

what i was suggesting, you have implement in your last post..

return false unless length(S1) == length(S2)

that is what i wanted to say you about checking the sets length first, which you forgot to mentioned in your earlier post...

this is very necessary, just take a look on below example..

L1 : 1 - 7 - 4 - 3 - 1

L2 : 4 - 1 - 7 - 4 - 3 - 50

It has one additional node in list L2, which shows difference in these list..

but any chance if you forget to put length condition it will return true.

which is wrong in our this case ... right?

0

but any chance if you forget to put length condition it will return true.

which is wrong in our this case ... right?

No. List contents can be *condensed* when converted to a set (due to duplicates being removed). Consider the following:

L1: 1 - 1 - 1 - 1 - 1 - 2

L2: 1 - 2

They are 'set equal'. This is, represented as sets, these two are equivalent; they both contain the values `[1,2]`

. However, if you take the length of them you get `length(L1) /= length(L2)`

which is * not* indicative of the set equality.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...