   Linked lists
  I was writing this phonebook- linked list classes for an exerciseand i got some bugs, so i need some help here..LINK TO THE CODE: http://rafb.net/paste/results/y7SAeh70.htmlfirst problum:i was getting a memory leak when i tryed to delete some onefrom the phone book list, because i only changed the myNext, myPrev..how can i fix it ?and the second problum was in the main functioncout << "\nInput first name: ";cin>>clientname;cout << "Input last name: ";cin>>lname;cout<<"Input phone number: ";cin>>phonenumber;when i was entring something like "la la"with space between, i was getting wierd resultsso i tryed cin.get();but it doesnt seem to be working with String variables.and i was getting some errors when tried to compile it.and i can also use some tips to build this program in a more effective way (remmber i am just exercising my programming skills), and i know about the "list" class.[Edited by - clearly on July 10, 2006 3:52:28 PM]
  cin will read in up to the first whitespace. Use getline().Edit:As for the memory leak, its becuase you simply move pointers around but you do not delete anything you new. Do something like this when deleteing a node:Node *tempNode = nodeToDelete;//move around next/previous ptrs so nodeToDelete is not in the listdelete tempNode;And just a design tip: I really don't think you need four different Node classes. Head and Tail nodes should be the same as internal nodes, just with NULL previous or next pointers. You can certainly keep a Head and Tail pointer, but those should just be regular old node pointers that point to your first/last node in the list.[Edited by - Driv3MeFar on July 10, 2006 5:37:55 PM];Quote:Original post by Driv3MeFarcin will read in up to the first whitespace. Use cin.getline().Edit:As for the memory leak, its becuase you simply move pointers around but you do not delete anything you new. Do something like this when deleteing a node:*** Source Snippet Removed ***And just a design tip: I really don't think you need four different Node classes. Head and Tail nodes should be the same as internal nodes, just with NULL previous or next pointers. You can certainly keep a Head and Tail pointer, but those should just be regular old node pointers that point to your first/last node in the list.where can i add this code?i tried to do something like this on the delete function:void InternalNode::Delete(Data* theData){if (myData->Equal(theData)){myNext->ChangeBefore(myPrev);myPrev->ChangeNext(myNext);cout << "\nDeleted succesfull..\n" << endl;delete myNext;delete myPrev;delete myData;}else myNext->Delete(theData);}and it crashed..what can i do?   ;  You could do something like this:void InternalNode::Delete(Data* theData){if (myData->Equal(theData)){myNext->ChangeBefore(myPrev); //next's previous points to one before thismyPrev->ChangeNext(myNext); //previous's next points to one after this delete this; //delete the current node}else myNext->Delete(theData);}Edit: As Captain P points out, you're crash was because you were deleteing too many things. Calling delete on your next and previous pointer will delete those nodes, which you do not want. All you want to remove is your current node, hence the delete this.Edit2 (More of a design point): I would recommend against making a recursive Delete function. If you are deleting the last element of a particularly long linked list, you could end up with quite a bit of overhead from the function calls. In the past when I've implemented linked lists, I would include a find() function that would return the # node in the list that contained a certain element. Then, for deletion, simply find() the element you want to delete, and pass that # into your Delete function (or, inside Delete(), find() the data you pass in). Once you know the node # you want to remove, you can simply walk the linked list in a loop until you reach the node, then delete it. Since you keep track of your head and tail, you can further optimize by either walking forward from the head or backward from the tail, depending on whether the node is in the first or second half of the list.[Edited by - Driv3MeFar on July 10, 2006 4:12:10 PM];  Note that you shouldn't delete myNext and myPrev. You don't want to remove those nodes. When you call 'delete this', you call the destructor, and that's where 'delete myData' should be.   ;  Instead of cin.getline() (which accepts only a char[]), use getline() in it's free form.  It works like this:istream &getline(const istream& in, std::string &data);// so you use it likestd::string name = "NO NAME";std::getline(cin, name);Otherwise you have to deal with char arrays and believe me, it's Ugly.[Edited by - agi_shi on July 10, 2006 6:42:50 PM];  thanks evryone.. the linked list is cool now.but about the getline function.. im still having some troublesint main(int argc, char* argv[]){LinkedList linked;string phonenumber,fname,lname;Data* pData;string mChoose;cout << "Linked lists on c++\n";for (;;mChoose="0"){cout << "1. New client\n2. Delete\n3. Show\n4. Exit.\n" << endl;cout << "Input number: ";getline(cin, mChoose);if (mChoose=="1") {cout << "\nInput first name: ";getline(cin, fname);cout << "Input last name: ";getline(cin, lname);cout<<"Input phone number: ";getline(cin, phonenumber);pData=new Data(fname,lname,phonenumber);linked.Insert(pData);}else if (mChoose=="2") {cout << "\nInput first name to delete: ";getline(cin, fname);cout << "Input last name to delete: ";getline(cin, lname);cout<<"Input Phone number to delete: ";getline(cin, phonenumber);pData=new Data(fname,lname,phonenumber);linked.Delete(pData);}else if (mChoose=="3") linked.ShowAll();else if (mChoose=="4") break;}return 0;}i re-wrote the main code, and thats how it looks now..anyway im still having some troubles when i try to run it.im getting wierd results.its not working the way it shouldfor example:Input number:(im pressing 4)and its still in the loop and asking me agianto input number.and it doesnt matter what i will press, it will still exit, even if i justpress enter.what can i do?   ;  You don't have to use getline() everywhere, just in fields where you want to include whitespace. I would use cin as normal for inputting you choice, and just use getline() for the strings.I haven't done this in a while, but I think if you input a number and hit enter cin will read up to the newline, but not including. Therefor, you may have to flush cin before using getline, if you use cin>> for your choice input.   ;Quote:Original post by clearlyi re-wrote the main code, and thats how it looks now..anyway im still having some troubles when i try to run it.im getting wierd results.its not working the way it shouldfor example:Input number:(im pressing 4)and its still in the loop and asking me agianto input number.and it doesnt matter what i will press, it will still exit, even if i justpress enter.what can i do?It's better to parse the input string into an integer.bool done = false;while(!done) //better to use a while loop here{cout << "1. New client\n2. Delete\n3. Show\n4. Exit.\n" << endl;cout << "Input number: ";int mChoose; //use an integer instead of stringcin >> mChoose; //don't need to use getlineswitch(mChoose) //instead of if statement{case 1:break;case 2:break;case 3:break;case 4:done = true;}};int main(int argc, char* argv[]){LinkedList linked;string phonenumber,fname,lname;Data* pData;cout << "Linked lists on c++\n";bool done=false;while (!done) {cout << "1. New client\n2. Delete\n3. Show\n4. Exit.\n" << endl;cout << "Input number: "; int mChoose;cin >> mChoose; switch (mChoose) {case 1:cout << "\nInput first name: "; getline(cin, fname);cout << "Input last name: "; getline(cin, lname);cout<<"Input phone number: "; getline(cin, phonenumber);pData=new Data(fname,lname,phonenumber);linked.Insert(pData);break;case 2: cout << "\nInput first name to delete: "; getline(cin, fname);cout << "Input last name to delete: "; getline(cin, lname);cout<<"Input Phone number to delete: "; getline(cin, phonenumber);pData=new Data(fname,lname,phonenumber);linked.Delete(pData);break;case 3: linked.ShowAll(); break;case 4: done=true; break;}}return 0;}as u see it didnt helped much either, i also tried to flush cout butit didnt help too..ialso tried to flush cinlike that:cin.flush();but i was getting this error:error C2039: 'flush' : is not a member of 'basic_istream<char,struct std::char_traits<char> >'Error executing cl.exe.so i tryedfflush(stdin);after the cin>>mChoose;but it didnt work either..what can i do?   