c++ - Linked List Exception Thrown -
i new linked lists , getting error when trying remove 1 of nodes of linked list.
exception thrown: exception thrown: read access violation. std::_string_alloc<std::_string_base_types<char,std::allocator<char> > >::_mysize(...) returned 0xddddddf1. occurred
code:
#include "stdafx.h" #include <iostream> #include <string> using namespace std; struct node { string song, artist; node* next; }; node* add(node *head) { string song, artist; cout << "enter song name:" << endl; getline(cin, song); cout << "enter artist name:" << endl; getline(cin, artist); node *new_ptr = new node; new_ptr->song = song; new_ptr->artist = artist; if (head == nullptr) { head = new_ptr; head->next = nullptr; } else { node *ptr = head; while (ptr->next != nullptr) { ptr = ptr->next; } ptr->next = new_ptr; ptr->next->next = nullptr; } cout << "song added." << endl; return head; } node* remove(node *head) { if (head == nullptr) { cout << "there no songs." << endl; return head; } string song_to_remove; bool found = false; cout << "enter song name remove:" << endl; getline(cin, song_to_remove); if (head->song.compare(song_to_remove) == 0) { found = true; node *temp = head; head = head->next; delete temp; } else if(head->next != nullptr) { node *prev_ptr = head; node *ptr = head->next; while (ptr != nullptr) { if (ptr->song.compare(song_to_remove) == 0) { found = true; node *temp = ptr; prev_ptr->next = ptr->next; delete temp; } ptr = ptr->next; prev_ptr = prev_ptr->next; } } if (!found) { cout << "song not found." << endl; } else { cout << "song removed." << endl; } return head; } void print(node *head) { node* ptr = head; if (ptr == nullptr) { cout << "there no songs added yet." << endl; } else { while (ptr != nullptr) { cout << ptr->song << " " << ptr->artist << endl; ptr = ptr->next; } } } int main() { node *head = nullptr; int option; while (1) { cout << "choose option: add song (1), remove song (2), or list songs (3)." << endl; cin >> option; cin.ignore(); if (!(option == 1 || option == 2 || option == 3)) { cout << "must pick either 1, 2, or 3." << endl; continue; } if (option == 1) { head = add(head); } else if (option == 2) { head = remove(head); } else if (option == 3) { print(head); } } return 0; }
i have working besides remove() function, , errors when have more 1 item in linked list , when try remove node besides first one.
i got it. didn't have break
statement after deleting ptr
, trying assign ptr
. trying make work if had multiple songs same name delete of them, program doesn't need specific.
node* remove(node *head) { if (head == nullptr) { cout << "there no songs." << endl; return head; } string song_to_remove; bool found = false; cout << "enter song name remove:" << endl; getline(cin, song_to_remove); if (head->song == song_to_remove) { found = true; node *temp = head; head = head->next; delete temp; } else if(head->next != nullptr) { node *prev_ptr = head; node *ptr = head->next; while (ptr != nullptr) { if (ptr->song == song_to_remove) { found = true; prev_ptr->next = ptr->next; delete ptr; break; } ptr = ptr->next; prev_ptr = prev_ptr->next; } } if (!found) { cout << "song not found." << endl; } else { cout << "song removed." << endl; } return head; }
wiki
Comments
Post a Comment