*
zf*****@mail.com:
On Apr 9, 2:56 am, "Alf P. Steinbach" <a...@start.nowrote:
>* zfar...@mail.com:
>>>
I have this function that searches a link list before main performs
the insert function to insert an item. My problem is the pointer stuff
and the program keeps crashing.
<code>
List::List()
{
listPtr = NULL;
}
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
while(loopPtr != NULL) // but listPtr is NULL- is this the
problem
{
if(loopPtr->item == Item)
return true;
else
{
loopPtr = loopPtr->next;
}
return false;
}
}
// this is the declaration in the spec file
private:
NodeType* listPtr;
int length;
</code>
Can anyone help?
The error's on line 162. In addition you forgot to specify a return
value in your IsThere function, for the case of listPtr == 0.
Pardon me but could you clarify about line 162. When I put in the
return value false for the case =0; the insert function skips.
Here is the other code
<code>
infile >Item;
if(list1.IsThere(Item))
{
while(infile)
{
if(list1.IsThere(Item))
{
list1.Insert(Item);
infile >Item;
}
}
}
else
cout << "Item duplicate!" << endl;
</code>
Please don't quote signatures -- corrected.
Line 162 is part of the code you didn't post.
Now, regarding the missing return value specification in IsThere, I
listed that as one error because the other error in IsThere, the early
return after checking the first node, is really the same error. To find
that error, execute the code on paper (play at being the computer).
What happens when loopPtr is 0 (execute the code on paper to see)? What
happens when the item to be found is in the second or third node (ditto)?
Btw., for the code you posted this time, is it really your intention to
only insert items that already are in the list?
Or did you forget a couple of logical "not"s?
Thank you.
You're welcome. Just don't quote signatures.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?