Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > linked list search

Reply
Thread Tools

linked list search

 
 
zfareed@mail.com
Guest
Posts: n/a
 
      04-09-2007
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-09-2007
* http://www.velocityreviews.com/forums/(E-Mail 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.

--
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-09-2007
(E-Mail Removed) wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
zfareed@mail.com
Guest
Posts: n/a
 
      04-09-2007
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?


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-09-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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);

>
>
> }
> </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?
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments