I am having trouble comparing two linked lists, I have 2 lists for arguments sake list1
contains {1,2,3,4,5}
and list2
contains {1,3,4,5,6}
I am not using linkedlistnodes
for a start which is why I am asking the question here, I did try switch to notes but a lot of my other work would then have to be re-written to make it work which I don't really want to do.
Anyway here is my code thus far, I am trying to use 2 loops to cycle and compare each value. The problem is that it doesn't work in the way I intended as I didn't think that it will compare the first value of list1
against all values in list2
before moving on. It stumped me how to get this working or if I am even going about it in the right way.
bool c = false;
foreach (int s in list1) {
foreach (int t in list2)
if (s == t) {
c = true;
//The console write line in this part of the code is for testing
Console.WriteLine("Both Lists Match {0}, {1}", s, t);
break;
} else {
c = false;
//The console write line in this part of the code is for testing
Console.WriteLine("Not a Match {0}, {1}", s, t);
}
}
if (c == true) {
Console.WriteLine("Both Lists Match");
} else {
Console.WriteLine("Not a Match");
}