Double Linked List Deleting Node with Index C++ -


[enter link description here][1]so, i've been having issues program i'm working on. i'm new programming, i'm determined in learning language.

i have use these 3 different structs in program, pointers starting trip me up. function must return new updated songlist. what have written far. problem nothing getting deleted, it's being overwritten within function. if please show how appreciated. i've taken out few of functions , switch statements condense post. main goal when remove function selected ask user pick index of song it'd deleted. when function called it'll take index , songlist parameters. remove node , return list.

i have use these 3 different structs in program, pointers starting trip me up.

well, starters, in c++, should using std::list class, , let manage pointers you.

the function must return new updated songlist.

you not returning updated list, returning first node in updated list. in of redundant, because returning same field assigning returned value to. return value not necessary, since caller knows list being modified.

the problem nothing getting deleted, it's being overwritten within function.

your removesong() implemented overly complicated needs do. not managing node pointers correctly, , not delete'ing memory, either.

also, f case not clearing list @ all, deleting first node without regard subsequent nodes may present, leaked in memory. proper clear algorithm needs loop through entire list deleting each node.

if please show how appreciated.

try this:

#include <iostream> #include <string>  using namespace std;  struct song {     int id;     string name;     string singername; };  struct songnode {     song sg;     songnode *previousnode;     songnode *nextnode; };  struct songdoublylinkedlist {     songnode *firstelement;     songnode *lastelement; };  void addsong(songdoublylinkedlist *songlist); void displaylistelements(songdoublylinkedlist *songlist); void displaylastelement(songdoublylinkedlist *songlist); void removesong(songdoublylinkedlist *songlist, int index); void clearlist(songdoublylinkedlist *songlist);  int main() {     songdoublylinkedlist songlist;     songlist.firstelement = null;      songlist.lastelement = null;      bool question = true;      while (question == true)     {         char letter;         cout << "\nenter letter of you'd next: " << endl;         cout << " = add new song" << endl;         cout << " b = display list of songs" << endl;         cout << " c = terminate program" << endl;         cout << " d = display last song in list" << endl;         cout << " e = delete song" << endl;         cout << " f = clear songs" << endl;         cin >> letter;          switch (letter)         {             case 'a':             {                  addsong(&songlist);                 break;             }              case 'b':              {                 displaylistelements(&songlist);                 break;             }              case 'c':             {                 question = false;                 break;             }              case 'd':             {                 displaylastelement(&songlist);                 break;             }              case 'e':             {                 int indexnumber;                 cout << "here ";                 displaylistelements(&songlist);                 cout << "enter index of song you'd delete ";                 cout << "(first song = 0)" << endl;                 cout << "enter here: ";                 cin >> indexnumber;                 removesong(&songlist, indexnumber);                 break;             }              case 'f':             {                 clearlist(&songlist);                 break;             }         }     }     return 0; }  void addsong(songdoublylinkedlist *songlist) {     songnode *songtemp = new songnode;     songtemp->previousnode = null; // note: important!     songtemp->nextnode = null; // note: important!      cout << "enter new song's id: ";     cin >> songtemp->sg.id;     cout << "enter new song's name: ";     cin >> songtemp->sg.name;     cout << "enter singer's name: ";     cin >> songtemp->sg.singername;      if (songlist->firstelement == null)         songlist->firstelement = songtemp;      if (songlist->lastelement != null)     {         songlist->lastelement->nextnode = songtemp;         songtemp->previousnode = songlist->lastelement;     }      songlist->lastelement = songtemp; }  void displaylistelements(songdoublylinkedlist *songlist) {     cout << "your list: " << endl;      songnode *temp = songlist->firstelement;     while (temp != null)     {         cout << temp->sg.id << endl;         cout << temp->sg.name << endl;         cout << temp->sg.singername << "\n" << endl;         temp = temp->nextnode;     }      cout << endl; }  void displaylastelement(songdoublylinkedlist *songlist) {     songnode *lastsong = songlist->lastelement;     if (lastsong == null)     {         cout << "your song list empty. " << endl;         return;     }      cout << "your last song : " << endl;     cout << lastsong->sg.id << endl;     cout << lastsong->sg.name << endl;     cout << lastsong->sg.singername << endl; }  void removesong(songdoublylinkedlist *songlist, int index) {     if (songlist->firstelement == null)     {         cout << "your song list empty. " << endl;         return;     }      songnode *node = songlist->firstelement;     for(int = 0; < index; ++i)     {         node = node->nextnode;         if (node == null)         {             cout << "invalid index. " << endl;             return;         }     }      if (node->previousnode != null)         node->previousnode->nextnode = node->nextnode;      if (node->nextnode != null)         node->nextnode->previousnode = node->previousnode;      if (songlist->firstelement == node)         songlist->firstelement = node->nextnode;      if (songlist->lastelement == node)         songlist->lastelement = node->previousnode;      delete node; }  void clearlist(songdoublylinkedlist *songlist) {     songnode *node = songlist->firstelement;     songlist->firstelement = null;     songlist->lastelement = null;      while (node != null)     {         songnode *temp = node->nextnode;         delete node;         node = temp;     } } 

alternatively, using std::list:

#include <iostream> #include <string> #include <list> #include <algorithm>  using namespace std;  struct song {     int id;     string name;     string singername; };  typedef std::list<song> songlist;  void addsong(songlist *songlist); void displaylistelements(songlist *songlist); void displaylastelement(songlist *songlist); void removesong(songlist *songlist, int index); void clearlist(songlist *songlist);  int main() {     songlist songlist;     bool question = true;      while (question == true)     {         char letter;         cout << "\nenter letter of you'd next: " << endl;         cout << " = add new song" << endl;         cout << " b = display list of songs" << endl;         cout << " c = terminate program" << endl;         cout << " d = display last song in list" << endl;         cout << " e = delete song" << endl;         cout << " f = clear songs" << endl;         cin >> letter;          switch (letter)         {             case 'a':             {                  addsong(&songlist);                 break;             }              case 'b':              {                 displaylistelements(&songlist);                 break;             }              case 'c':             {                 question = false;                 break;             }              case 'd':             {                 displaylastelement(&songlist);                 break;             }              case 'e':             {                 int indexnumber;                 cout << "here ";                 displaylistelements(&songlist);                 cout << "enter index of song you'd delete ";                 cout << "(first song = 0)" << endl;                 cout << "enter here: ";                 cin >> indexnumber;                 removesong(&songlist, indexnumber);                 break;             }              case 'f':             {                 clearlist(&songlist);                 break;             }         }     }     return 0; }  void addsong(songlist *songlist) {     song songtemp;      cout << "enter new song's id: ";     cin >> songtemp.id;     cout << "enter new song's name: ";     cin >> songtemp.name;     cout << "enter singer's name: ";     cin >> songtemp.singername;      songlist->push_back(songtemp); }  void displaylistelements(songlist *songlist) {     cout << "your list: " << endl;      songlist::iterator iter = songlist->begin();     while (iter != songlist->end())     {         cout << iter->id << endl;         cout << iter->name << endl;         cout << iter->singername << "\n" << endl;         ++iter;     }      cout << endl; }  void displaylastelement(songlist *songlist) {     if (songlist->empty())     {         cout << "your song list empty. " << endl;         return;     }      songlist::reverse_iterator iter = songlist->rbegin();     cout << "your last song : " << endl;     cout << iter->id << endl;     cout << iter->name << endl;     cout << iter->singername << endl; }  void removesong(songlist *songlist, int index) {     if (songlist->empty())     {         cout << "your song list empty. " << endl;         return;     }      songlist::iterator iter = std::advance(songlist->begin(), index);     if (iter == songlist->end())     {         cout << "invalid index. " << endl;         return;     }      songlist->erase(iter); }  void clearlist(songlist *songlist) {     songlist->clear(); } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -