c++ - DoublyLinkedList. Does nothing when trying to insert between 2 nodes -
im having problem dll insert function. has no problem appending onto list when have list of 5 objects , want insert in between nodes nothing. have been looking around hours resolve , nothing has changed.
here code: implementation in main.cpp
cout << "\n---------------------------insert---------------------------\n"; //ask user position want enter player in. cout << "enter position want enter player: "; int insertchoice = validators.getnum(); //move iterator start. itr.start(); //if position not equal zero, continue. if (insertchoice != 0) { //loop until position entered reached. (int = 0; < insertchoice; i++) { //move iterator forware position. itr.forth(); } //insert player position entered user. //list.insert(itr,stats.input()); } //if position entered equal zero, continue. else { //append onto list.(add onto start). list.append(stats.input()); }
insert function in header file.
// ---------------------------------------------------------------------------------------------------------------- // name: insert // description: inserts data before iterator, works whether iterator backwards of forwards // through list.inserts @ end of list if iterator invalid. // arguments: p_iterator: iterator insert before // p_data: data insert // return value: none. // ---------------------------------------------------------------------------------------------------------------- void insert(doublylinkedlistiterator<datatype>& p_itr, datatype p_data) { if(p_itr.m_node != 0) { // insert data before iterator p_itr.m_node->insertbefore(p_data); //if iterator @ head of list, //reset head pointer if(p_itr.m_node == m_head) { m_head = m_head->m_prev; } // increment count m_count++; } else { append(p_data); } }
append
void append(datatype p_data) { if(m_head == 0) { // create new head node. m_head= m_tail= new doublylistnode<datatype>; m_head->m_data= p_data; m_head->m_next= 0; m_head->m_prev= 0; } else { // insert new node after tail , reset tail. m_tail->insertafter(p_data); m_tail= m_tail->m_next; } m_count++; }
insertbefore
// ---------------------------------------------------------------- // name: insertbefore // description: adds node before current node. // arguments: p_data - data store in new node. // return value: none. // ---------------------------------------------------------------- void insertbefore(datatype p_data) { //create new node doublylinkedlistnode<datatype>* newnode = new doublylinkedlistnode<datatype>; newnode->m_data = p_data; //set new node pointers newnode->m_next = this; newnode->m_prev = m_prev; //if theres node before it, make point new node if(m_prev != 0) m_prev = newnode; }
i dont think problem lies int implementation in main rather in actual insert function. in advance, becca.
insertbefore
newnode->m_next = this; newnode->m_prev = this->m_prev;
you need tell this
point (as previous) new node too.
newnode->m_next = this; newnode->m_prev = this->m_prev; this->m_prev->m_next = newnode; this->m_prev = newnode;
this should be
insert function in header file.
if(p_itr.m_node == m_head) { m_head = p_itr->m_prev; ^^^^^^ }
Comments
Post a Comment