Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 3rd, 2005, 7:28 PM   #1
kevinbelonzi
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 kevinbelonzi is on a distinguished road
Question Identical Lists

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?
kevinbelonzi is offline   Reply With Quote
Old Feb 3rd, 2005, 9:20 PM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
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:
simple pseudocode

good luck

Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Feb 4th, 2005, 10:22 AM   #3
kevinbelonzi
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 kevinbelonzi is on a distinguished road
Question

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;
}
kevinbelonzi is offline   Reply With Quote
Old Feb 4th, 2005, 10:25 AM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
you can test it by making a test class and using it, if(list1.identical(list2)) printf("same"); something like that.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Feb 4th, 2005, 11:03 AM   #5
kevinbelonzi
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 kevinbelonzi is on a distinguished road
Ok, but in my test should I have the user create two lists, then test them to see if they're identical?
kevinbelonzi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC