linked list search

Discussion in 'C++' started by zfareed@mail.com, Apr 9, 2007.

  1. Guest

    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
     
    , Apr 9, 2007
    #1
    1. Advertising

  2. * :
    > 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?
     
    Alf P. Steinbach, Apr 9, 2007
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Apr 9, 2007
    #3
  4. Guest

    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?
     
    , Apr 9, 2007
    #4
  5. Jim Langston Guest

    <> wrote in message
    news:...
    > 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?
    >
    >
     
    Jim Langston, Apr 9, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    485
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    474
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    515
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    675
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    765
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page