![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
I'm working with Sorted Linked Lists in a c++ class and am trying to figure out how to add a function that determines whether list1 and list2 are identical. Can anyone help?
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
you'll have to write a method that makes two cursors, that each point to a list, then they iterate through the list and compare the current nodes.
cursor1 = list1.head;
cursor2 = list2.head;
if (list1.length != list2.length) return FALSE;
while(cursor1 != NULL || cursor2 != NULL)
{
if (cursor1.data != cursor2.data) return FALSE;
cursor1 = cursor1.next;
cursor2 = cursor2.next;
}
return TRUE:good luck Dizz |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
I have that method set up in a template in a header file, like this: Does this look correct: And: how would I test this method in a .cpp file
template <class ItemType> bool LinkedList<ItemType>::Identical(LinkedList<ItemType> list) const { NodeType<ItemType>* selfPtr; NodeType<ItemType>* otherPtr; bool same = true; selfPtr = headoflist; otherPtr = list.headofList; while (same) { same (selfPtr != NULL && otherPtr != NULL && selfPtr- >info == otherPtr->info); if (same) { selfPtr = selfPtr->next; otherPtr = otherPtr->next; } } if (selfPtr == NULL && otherPtr == NULL) return true; else return false; } |
|
|
|
|
|
#4 |
|
Professional Programmer
|
you can test it by making a test class and using it, if(list1.identical(list2)) printf("same"); something like that.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
Ok, but in my test should I have the user create two lists, then test them to see if they're identical?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|