linked list search

Z

zfareed

I am attempting to search a list to check whether an item is already
present before inserting. I am having trouble with the pointers; I
understand that I need to set the pointer to the start of list but I
keep getting compilation errors. Can anyone help? Here is my code:
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
if (Item == listPtr->item)
return true;
else
{// Search for node in rest of list

while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;
}

</code>

the program crashes on execution
 
A

Alf P. Steinbach

* (e-mail address removed):
I am attempting to search a list to check whether an item is already
present before inserting. I am having trouble with the pointers; I
understand that I need to set the pointer to the start of list but I
keep getting compilation errors. Can anyone help? Here is my code:
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
if (Item == listPtr->item)
return true;
else
{// Search for node in rest of list

while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;
}

</code>

the program crashes on execution

What happens when loopPtr is 0?

Walk through the code's execution to find out.

What happens when loopPtr isn't 0, and the searched for item is in the
first node?

Walk through the code's execution to find out.

What happens when loopPtr isn't 0, and the searched for item is not in
the first node?

Walk through the code's execution to find out.
 
V

Victor Bazarov

I am attempting to search a list to check whether an item is already
present before inserting. I am having trouble with the pointers; I
understand that I need to set the pointer to the start of list but I
keep getting compilation errors. Can anyone help? Here is my code:
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
if (Item == listPtr->item)
return true;
else
{// Search for node in rest of list

while (loopPtr->next->item != Item)

This presumes the item is there. What if it isn't? How do you stop
at the end of the list?
loopPtr = loopPtr->next;
return true;
}
return false;
}

I'd probably rewrite it as

NodePtr loopPtr = listPtr;
while (loopPtr) {
if (loopPtr->item == Item)
return true;
loopPtr = loopPtr->next;
}
return false;
</code>

the program crashes on execution

V
 
Z

zfareed

I'm sorry guys, I promise this is the last question. Here is my code
in main:

<code>
infile >> Item;

if(list1.IsThere(Item))
cout << "Item duplicate!" << endl;
else
{ list1.Insert(Item);
infile >> Item;
while(!(list1.IsThere(Item)))
{ while(infile)
{
if(!(list1.IsThere(Item)))
{
list1.Insert(Item);
infile >> Item;
}
infile >> Item
}


}
</code>

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

loopPtr = listPtr;

while(loopPtr)
{

if (loopPtr->item == Item)
{
return true;
}
loopPtr = loopPtr-> next;
}

return false;


}
</code>

Will this work?
 
J

Jim Langston

I'm sorry guys, I promise this is the last question. Here is my code
in main:

<code>
infile >> Item;

if(list1.IsThere(Item))
cout << "Item duplicate!" << endl;
else
{ list1.Insert(Item);
infile >> Item;
while(!(list1.IsThere(Item)))
{ while(infile)
{
if(!(list1.IsThere(Item)))
{
list1.Insert(Item);
infile >> Item;
}
infile >> Item
}

This block of code is confusing. I'm not quite sure what you are trying to
accomplish, as you are reding input in 3 different places, inserting in 2,
checking for duplicates in 3, and only displaying "Item Duplicate!" once.

Are you tryign to read the items until the end and simply not entering the
item if it's a duplicate, but then reading the rest? Or are you trying to
stop on any duplicate?

Wouldn't something like this be better?

while ( infile >> item )
if ( list1.IsThere(Item) )
{
cout << "Item Duplicate!" << endl;
// break
// Uncomment break if you want to halt on any duplicate
}
else
list1.Insert(Item);
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top