c++ - Deleting nodes from LinkedList -
c++ - Deleting nodes from LinkedList -
just working on improve understanding linked list behavior. sense logic right, i'm not getting what's going on. programme breaking when nail point in main deleting node list. broke, "exe" file in release folder (vs2013) missing , cant run programme debugging. idk happened there, , don't know why isn't working. help amazing!
when run gets hung on list1.remove(node_to_remove) , debugger cant run program.
linkedlist.h:
class linkedlist { protected: struct listnode { double value; listnode *next; listnode(double value1, listnode *next1 = null) { value = value1; next = next1; } }; listnode *head; public: // class constructor(s) linkedlist() { head = null; } linkedlist(const linkedlist &); void add(double x); void print() const; void linkedlist::remove(double value); private: }; linkedlist.cpp:
// re-create constructor linkedlist::linkedlist(const linkedlist &object) { head = object.head; } void linkedlist::add(double x) { if (head == null) head = new listnode(x); else { listnode *nodeptr = head; while (nodeptr->next != null) nodeptr = nodeptr->next; nodeptr->next = new listnode(x); } } void linkedlist::print() const { listnode *nodeptr = head; while (nodeptr) { std::cout << nodeptr->value << ", "; nodeptr = nodeptr->next; } } void linkedlist::remove(double value) { listnode *nodeptr = head; listnode *ptrdel = null; ptrdel = nodeptr; while (ptrdel != null) { if (nodeptr->value == value) { ptrdel = nodeptr; nodeptr = ptrdel->next; delete ptrdel; break; } } } main:
int main() { const double node_to_remove = 15.23; const double list1_nodes[] = { 67.5, -7.8, node_to_remove, 98.76, -19.45 }; const double nonexistent_node = 45.76; const int size = sizeof(list1_nodes) / sizeof(list1_nodes[0]); linkedlist list1; linkedlist list3; linkedlist list4; (int index = 0; index < size; ++index) list1.add(list1_nodes[index]); cout << " \"list1\" original: "; list1.print(); //copy constructor test linkedlist list2(list1); cout << " \"list2\" original: "; list2.print(); // node removal test: list1.remove(node_to_remove); cout << "\n \"list1\" modified: "; list1.print(); cout << "\n \"list2\" unchanged: "; list2.print(); cout << endl; system("pause"); homecoming 0;
generally speaking when want delete node in linked list want find first. assuming have data in node want delete, algorithm is:
head loop until find node contains data point previous node's next node holding data 's next now delete node containing data note: have special case when info in head itself.
in c++ done in code below:
bool deletenode(datatype data) //true if node found , deleted, false if not found { if(head == 0) homecoming false; //list empty if(head->next == 0 && head->data == data) //when thge head node deleted. { head = 0; //set head null. safer `delete head;` homecoming true; } node *tmp, *prv; tmp = prv = head; while(tmp != 0) { if(tmp->data == data) { prv->next = prv->next->next; delete tmp; homecoming true; } prv = tmp; tmp = tmp->next; } homecoming false; } c++ linked-list nodes
Comments
Post a Comment