c++ - Creating and linkin doubly linked list -


hi have been trying debug piece of code long time cant figure out why. appreciated.

here, trying copy 2 doubly linked lists(of different lengths) new one. however, when return newly made linked list, nodes not connected. can tell me have done wrong?

struct polynode {     int coef;     int expx;     int expy;     polynode* prev;     polynode* next; }  polynode* padd(polynode* a, polynode* b) {     polynode* c = new polynode;     polynode* c_head =c;     polynode* a_head =a;     polynode* b_head =b;     c->prev = nullptr;     c->next = nullptr;      polynode* c_next = c->next;     polynode* c_prev = c->prev;      while (a != nullptr)     {         c->coef = a->coef;         c->expx = a->expx;         c->expy = a->expy;         cout << "\t\t copied c=" << c->coef << c->expx << c->expy << endl;          if(a->next != nullptr)         {             c_next = new polynode;             c_next->prev = c;             c_prev = c;             c = c_next;             = a->next;         }         else         {             c_next = new polynode;             c_next->prev = c;             c_prev = c;             c = c_next;             break;         }     }      while (b != nullptr)     {         c->coef = b->coef;         c->expx = b->expx;         c->expy = b->expy;         cout << "\t\t copied c=" << c->coef << c->expx << c->expy << endl;          if(b->next != nullptr)         {             c_next = new polynode;             c_next->prev = c;             c = c_next;             c_prev = c;             b = b->next;         }         else         {             c_next = nullptr;             break;         }     }      c_next = nullptr;      int sum = polylength(a_head) + polylength(b_head);      for(int =0; i< sum-1 ; i++)     {         if(c_prev == nullptr)         {             break;         }          c_next = c;         c = c_prev;         c_prev = c_prev->prev;     }      c_next = c;     c = c_prev;     c_prev = nullptr;      sortpoly(c); //sortpoly function sorts polynomials largest smallest exponents , sum them if same exponents.     return c;  } 

also not experienced programmer , trying develop programming habits. please suggest common best practice dealing pointers , linked list. thanks! problem here in beginning of function things go bad , returned node not have links next node.

[edit stuyckp] indented code , added problem statement last sentence.

this code full of bugs, if learning work pointers have go 1 step @ time , make drawing of pointers. did verify code , problems pop out pretty quickly.

your basic error seem think following :

polynode* c_next = c->next; polynode* c_prev = c->prev; 

will somehow create variable still belonging c node, while in fact value of c->next null , value copied in new variable. if change new variable, not changing c->next @ all.

i not going solve problems step in right direction. don't use these temporaries, instead :

while (a != nullptr) {     c->coef = a->coef;     c->expx = a->expx;     c->expy = a->expy;     cout << "\t\t copied c=" << c->coef << c->expx << c->expy << endl;      if(a->next != nullptr)     {         c->next = new polynode;//make c's next point new node         c->next->prev = c; //make new nodes prev point c         c->next->next = nullptr; //make sure indicate end         c = c_next; //now move c next position         = a->next; //now move next     }      ... 

i still don't code though, because if , b both null still allocate new node. allocate within while loop. , initialise c null @ start.

your code doing way many thinks make debugging , maintaining crazy. why there copy paste code while loop , while b loop practically same. try make fine grained functions names document doing within them. padd instance bad name large function. not adding copying 2 lists , creating new 1 both of them.

this not c++ either, plain c. in c++ use different approach behavior added datastructure in of methods. won't elaborate on this, think have enough go on.


Comments

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -