Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Lynncake

  • 3 years ago

how do you search for a lastname in a linked list? c++

  • This Question is Closed
  1. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i have a linked list that stores firstname, lastname, and phone number. i need to find the lastname

  2. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Are you using the STL?

  3. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I am using visual studio c++

  4. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Did you implement the linked list yourself?

  5. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i did. ammm can you give me an example of a referenced variable?

  6. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Referenced variable is like a pointer that is pretending to be deferenced.

  7. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Okay I need to know what your code is like first of all.

  8. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    bool PhoneBook::Lookup (const string& lname, const string& fname, string& pnum) const { PhoneBookItem* p = head; PhoneBookItem* q = head->next; if (head == NULL) return false; else if (lname == p->lastname) { if (fname == p->firstname) { return true; } else return false; } else { while (q != NULL && lname != q->lastname) { p = q; } if (q != NULL && lname == q->lastname) if (fname == q->firstname) return true; else return false; else return false; } }

  9. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It says that the pnum should be returned to the calling function via a third argument, a reference variable. I don't know how. can you help me with this?

  10. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    What is pnum supposed to be?

  11. SnuggieLad
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    wio..phone number

  12. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    All you need to do is assign pnum to the pnum of the node with the correct name.

  13. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` while (q != NULL && lname != q->lastname) { p = q; } ``` This code doesn't make sense. You should try something like: ``` while (q != NULL && lname != q->lastname) { p = q; q = q->next; } ``` You need to update q and p.

  14. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    So assuming you find it, before you return true, you wanna assign pnum. ``` if (fname == p->firstname) { pnum = q->pnum; // put it here return true; } ``` ``` if (fname == q->firstname) { pnum = q->pnum; // here too return true; } else ```

  15. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I tried that, but it doesn't work. Here is the code in the main. void LookupEntry(const PhoneBook& pb) { string last, first, phone; cout << "Enter last name: "; cin >> last; cout << "Enter first name: "; cin >> first; bool success = pb.Lookup(last, first, phone); if (success) { cout << "\nThe phone number is " << phone << ". \n"; } else { cout << "\nError: no one by this name is in the phone book.\n"; } }

  16. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` string last, first, phone; ``` The problem is that `phone` should be a reference. ``` string last, first; string& phone; ```

  17. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Does it even compile?

  18. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It does, but that code in the main cannot be changed since our professor said so.

  19. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Okay, what sort of output are you getting? What is happening?

  20. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It outputs : Error: no one by this name is in the phone book. ================================================= if (success) { cout << "\nThe phone number is " << phone << ". \n"; } else { cout << "\nError: no one by this name is in the phone book.\n"; }

  21. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Alright, are you searching for something that is in the list?

  22. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes

  23. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` else if (lname == p->lastname) { if (fname == p->firstname) { return true; } else return false; } ``` This code is wrong. If the last name is correct but the first name is not correct, it should continue searching, but your code instead gives up entirely.

  24. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` else if (lname == p->lastname && fname == p->firstname) { return true; } ``` This is what you should be doing.

  25. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    There are a lot of bugs in your code, it'd be easier to show the correct way...

  26. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For example, why have `p` and `q`? You only need one to keep track of where you are, right?

  27. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yeah but i need to test this ============================================= if (head == NULL) return false; else if (lname == p->lastname && fname == p->firstname) { p->phone = head->phone; return true; } else { while (q != NULL && lname != q->lastname) { p = q; p = p->next; } if (q != NULL && lname == q->lastname && fname == q->firstname) { q->phone = head->phone; return true; } else return false; } ===================================== else { while (q != NULL && lname != q->lastname) { p = q; p = p->next; } if (q != NULL && lname == q->lastname && fname == q->firstname) { q->phone = head->phone; return true; } else return false; }

  28. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    why are you setting `p->phone` to `head->phone`?

  29. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    those are the variable in my struct ====================================================== struct PhoneBookItem { string lastname; string firstname; string phone; PhoneBookItem* next; PhoneBookItem(const string& l, const string& f, const string& ph); };

  30. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yeah, but you're setting the current item `p` to have the same phone number as the first item `head`. There is no reason to do that.

  31. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Okay, you should start out with your current item point to the front: ``` PhoneBookItem* current = head; ```

  32. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes i did that

  33. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    If you go through the whole list without finding it, then you want to return false. ``` while (current != NULL) { // search code here current = current->next } return false; ```

  34. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Does that loop make sense so far? @Lynncake

  35. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes it does, i'm writing the rest of it. but i am not sure if this is right

  36. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    PhoneBookItem* p = head; if (head == NULL) return false; else { while (p != NULL) { if (lname == p->lastname && fname == p->firstname) { return true; } p = p->next; } return false; }

  37. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Now, for the search code... we just check if the names are correct ``` if (current->firstname == fname && current->lastname == lname) { pnum = current->phone; return true; } ```

  38. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    We don't have anything if the names don't match. The loop will take care of that for us.

  39. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes it worked! thank you

  40. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    One second though.

  41. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` if (head == NULL) return false; else { while (p != NULL) ``` The `if (head == NULL)` here is pointless. The `while (p != NULL)` already check if `head` is `NULL`.

  42. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    @Lynncake Does that make sense?

  43. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes, it does thanks a lot

  44. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    alright, well good luck then.

  45. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    for the delete, is this right? ====================================== PhoneBookItem* p = head; while (p != NULL) { if (lname == p->lastname && fname == p->firstname) { delete p; p->next=NULL; num--; return true; } p = p->next; } return false; ====================================== I followed your method/concept.

  46. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    The problem with this is that you need to keep track of the item before you.

  47. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    And you set the guy behind you's `next` property to your own `next` property.

  48. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so in here i should hava another pointer that points to the next element?

  49. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    This case is a bit tricky because you have two case: 1) They are deleting the head... 2) They are deleting something in the middle of the list

  50. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For 1) you delete it, and set head to NULL For 2) you need to set the previous guy's next to the deleted guy's next

  51. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` PhoneBookItem* last = head; PhoneBookItem* current = head->next; // Check head // Check guys in the middle of the list ```

  52. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For check head, we wanna do: ``` if (head != NULL && head->firstname == fname && head->lastname == lname) { delete head; head = NULL; } ```

  53. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For checking others, we want a loop: ``` while (current != NULL) { \\ check current last = current; current = current->next; } return false; ```

  54. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For current, we do something similar to head, but with a change: ``` if (current->firstname == fname && current->lastname == lname) { last->next = current->next; delete head; return true; } ```

  55. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Hmmm, there should be a `return true;` in the check head part too.

  56. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    What we are doing is changing this: |dw:1364256150764:dw|

  57. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so the last is the head?

  58. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    To this: |dw:1364256188626:dw|

  59. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    `last` starts out as the head in the beginning. But last is set to `current` every time go move to the next element. `last` is just storing what comes before `current`

  60. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so i should create another pointer pointing to last?

  61. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yep

  62. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I gotta brb for a bit, but look at the code I posted if you are confused

  63. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i will, and thank you so much

  64. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes it did delte, but now my printing is messed up after deleting ============================================== ///////////////////////DELETE//////////////////////////////////////// PhoneBookItem* p = head; PhoneBookItem* q; int count=0; while (p != NULL) { if (count==0) { if (p->firstname == fname && p->lastname == lname) { delete head; head = NULL; num--; return true; } } else if (count>0) { if (p->firstname == fname && p->lastname == lname) { q->next = p->next; delete head; num--; return true; } } q = p; p = p->next; count++; } return false; ============================================= ///////////////////////////////PRINT///////////////////////////////// ostream& operator << (ostream& out, const PhoneBook& pbook) { PhoneBookItem* p = pbook.head; while (p != NULL) { out <<p->lastname<<", "<<p->firstname<<": "<<p->phone<<endl; p = p->next; } return out; }

  65. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    There is nothing wrong with printing. The linked list is getting ruined by your delete somehow.

  66. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` if (p->firstname == fname && p->lastname == lname) { q->next = p->next; delete head; num--; return true; } ``` You should be deleting `p` here not `head`

  67. wio
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Your count idea id pretty interesting, though not as efficient it gets the job done.

  68. Lynncake
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thank you so much. now i can study my other classes. ^_^ THANK YOU!!!!

  69. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy