Question

I am trying to search a name on a list and then remove it by invoking...

I am trying to search a name on a list and then remove it by invoking the remove function. However when I search for the name it keeps on saying name not found. What am I doing wrong? (The instruction is in the comments)

The remove and drop(delete) coding I have so far is below.

void buddyList::remove(buddy * r) {

buddy * b4 = head;

if (r == head)

head = head->next;

else {

while (b4->next != r) {

b4 = b4->next;

}

b4->next = r->next;

}

r->next = NULL;

} // remove()

//Given a pointer to a node in the list, removes the node from the list, but does NOT deallocate it.

int buddyList::drop(string fname, string lname) {

buddy* node = search(fname,lname);

int found = -1;

if (node = search(fname,lname)) {

found = 0;

remove(node);

node->~buddy();

return 0;

}

else

return -1;

}

//Given a the first and last name of a buddy to drop :

//search() for the name; if not found, return -1 for not found

//remove() the found node

//deallocate the found node(do not leave garbage!)

//return 0 indicating success

0 0
Add a comment Improve this question Transcribed image text
Answer #1

void buddyList::remove(buddy * r)

{

   buddy * b4 = head;

    if (r == head)

        head = head->next;

    else {

       

        while (b4->next != r) {

            b4 = b4->next;

        }

        b4->next = r->next;

    }

    r->next = NULL;

} // remove()

//Given a pointer to a node in the list, removes the node from the list,

// but does NOT deallocate it.

int buddyList::drop(string fname, string lname)

{

    // search() returns NULL if the node is not found

    buddy* node = search(fname,lname);

    int found = -1;

    // if the node is found in list

    if (node != NULL)

    {

        found = 0;

       

        // remove the node

        remove(node);

       

        // call the destructor

        node->~buddy();

       

        return found;

    }

    else

        return -1;

}

//Given a the first and last name of a buddy to drop :

//search() for the name; if not found, return -1 for not found

//remove() the found node

//deallocate the found node(do not leave garbage!)

//return 0 indicating success

Add a comment
Know the answer?
Add Answer to:
I am trying to search a name on a list and then remove it by invoking...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

  • In Java I am trying to search for a string in appList array and return the...

    In Java I am trying to search for a string in appList array and return the app name if found, if not return null. I have the following but get errors compiling: /*Find an app based on its name * @param name the name of the app to search for * @return the app with the given name * or null if there is no app with that name */ public App findApp(String name) { for (int i=0; i <...

  • Hello, can someone help me solve the remove function since when I try to remove it...

    Hello, can someone help me solve the remove function since when I try to remove it doesn't work, please and thank you. #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public:    typedef int data_t;    node *next;    data_t data;    node(data_t d) { next = NULL; data = d; } }; class linked_list { private:    node *head; public:    linked_list()    {        head = NULL;    }    // Get the...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT