c++ - Linked list (deleting a node) -


my question if user enters last name , there multiple same last name inside linked list , 1 of last name in head node. how going delete 1 of other last name without deleting head node. tried ways can think of desired node deleted (which good) including head node (which not want..)

void numberlist::deletecertainrecord() { string lname = "";  listnode *ptr; ptr = head; string answer; cout << "please enter last name: "<<endl; cin>>answer; int num = 0; char confirm; while (ptr!= null){     lname = ptr -> data.person.getlastname();      if(lname == answer){         num++;         cout << "person found : ";         cout << ptr->data.person.gettitle()<<" " << ptr -> data.person.getfirstname() << " " << ptr -> data.person.getlastname() << endl << endl;         cout << "do want delete his/her record? [y/n]" << endl;         do{             cin >> confirm;             if(confirm=='y'||confirm=='y'){                     listnode *previousnode;                      if(isempty()){                         return;                     }                      else if(head->data.person.getlastname() == answer){                         ptr = head->next;                         delete head;                          head = ptr;                     }                      else{                         ptr = head;                          while(ptr!=null && ptr->data.person.getlastname() != answer){                             previousnode = ptr;                             ptr = ptr->next;                         }                          if(ptr==null){                             cout<< "node not found!" << endl;                             return;                         }                         else{                             previousnode->next = ptr->next;                             delete ptr;                          }                     }                     cout << endl << "deleting..."<<endl;                     cout << "done!!" <<endl;                     cout << "exiting delete function... "<<endl;                     return;             }             else if(confirm=='n'||confirm=='n'){                 break;             }             else{                 cout << "invalid input, please enter again!!" << endl;             }         }while(confirm!='y'&&confirm!='y'&&confirm!='n'&& confirm!='n');         cout << endl;     }      ptr = ptr -> next; } if (num == 0){     cout << "\nno person last name ("<< answer << ") found!" << endl;     cout << "exiting delete function... "<<endl;     return; } } 

the sheer amount of code doing clouding actual intent.

  • you have linked list of people.
  • given last name want enumerate list looking matches.
  • upon discovering match, want prompt user deletion confirmation
  • if confirmed delete, extricate discovered node, leaving list otherwise-intact, , delete it.
  • else if not confirmed, skip next node, looking more matches

you're apparently having problem deletion portion if node is, in fact, head node. special-cased head-node logic tricky people new linked lists. fortunately can circumvented entirely if use right algorithm. assuming list terminated null, including null head pointer if list empty, such algorithm below:

listnode ** pp = &head; while (*pp) {     if ((*pp)->data.person.getlastname() == answer &&          confirmdelete((*pp)->data.person)) // <== todo: write function     {         listnode *victim = *pp;         *pp = victim->next;         delete victim;     }     else     {   // advance next person         pp = &(*pp)->next;     } } 

this delete user if they're first node in list and advance head pointer you. works on single-node lists both , without matching conditions, , empty lists head null. technique uses pointers in list mechanism enumerating list; not values, the actual pointers.

lastly, can made considerably more efficient if keep list sorted, leave you.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -