C++ Link List error going to next node -
i need read words file , put link list.
have these codes here(below) inserting link list part.
vector contains words i've read file
list( vector <string> &v ) { listnode *cur = head; ( int = 0 ; < v.size() ; i++ ) { //cout << v[i] << endl; listnode *newnode = new listnode; newnode->item = v[i]; if ( == 0 ) // first node { newnode->next = null; head = newnode; } else // insert node { listnode *prev = cur; newnode->next = prev->next; prev->next = newnode; } cout << cur->item << endl; system("pause"); } }
my problem here node doesn't go next node after inserting word node.
tried put cur = cur->next; on between cout << cur->item << endl; , system("pause");
program gives me error
you not updating child node correctly. try this, in else
block.
else { listnode *prev = cur; prev->next = newnode; } cur = newnode;
it can rewritten simpler. first try rectify existing program , go efficient one. hope helps !
Comments
Post a Comment