searching a linked list

Z

zfareed

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?
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
Z

zfareed

* (e-mail address removed):







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.

--
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?- Hide quoted text -

- Show quoted text -

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>

Thank you.
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
J

Jeff

* (e-mail address removed):







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.

--
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?- Hide quoted text -

- Show quoted text -

Hello Steinbach,
Yeah£¬ Pardon me too:) how did you know the error was in line 162.
 
Z

zfareed

Hi
Can you take a look at this code and check my error? I understand the
logic in what Steinbach is saying but why the errors?
<code>

bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;


if (Item == listPtr->item)
return true;
else
// Search for node in rest of list
{ loopPtr = listPtr;
while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;
}
</code>
the program just crashes
 
R

red floyd

Hi
Can you take a look at this code and check my error? I understand the
logic in what Steinbach is saying but why the errors?
<code>

bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;


if (Item == listPtr->item)
return true;
else
// Search for node in rest of list
{ loopPtr = listPtr;
while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
This line always gets executed.
 
Z

zfareed

I'm sorry; are you referring to the return true line? I am still lost,
would you suggest the correct code to perform this search? if the item
is found, return true else return false in order to continue inserting
an item in the list. This is the only part holding me up; I've even
printed the entire list.
 
J

James Kanze

Can you take a look at this code and check my error? I understand the
logic in what Steinbach is saying but why the errors?
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;

if (Item == listPtr->item)
return true;
else
// Search for node in rest of list
{ loopPtr = listPtr;
while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;}
</code>
the program just crashes

You missed one important point in what he said: execute the code
in your head. Play at being the machine.

And while you're at it, consider whether you really need any
special cases, or those premature returns. This is, after all,
a linear search, and the standard algorithm for a linear search
is just an empty while loop, followed by a return.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top