2017-08-23 15 views
-1

私はリンクされたリストで新しく、リンクされたリストのノードの1つを削除しようとするとこのエラーが発生します。リンクされたリストの例外がスローされました

Exception thrown: Exception thrown: read access violation. 
std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >::_Mysize(...) returned 0xDDDDDDF1. occurred 

コード:

#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 are no songs." << endl; 
     return head; 
    } 

    string song_to_remove; 
    bool found = false; 

    cout << "Enter song name to 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 are no songs added yet." << endl; 
    } 
    else 
    { 
     while (ptr != nullptr) 
     { 
      cout << ptr->song << " by " << ptr->artist << endl; 
      ptr = ptr->next; 
     } 
    } 
} 

int main() 
{ 
    node *head = nullptr; 
    int option; 

    while (1) 
    { 
     cout << "Choose an option: Add a song (1), remove a song (2), or list all the 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; 
} 

私はリンクリストで1つの以上のアイテムを持っているとき、私はノードを削除しようとすると、私がremove()関数以外の作業はすべて、それだけでエラーを持っています最初のものに加えて。

+2

エリックリペットで(https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)[小さなプログラムをデバッグする方法]読み込むための最適な時間です、このようなクラッシュをキャッチするためにデバッガを使用する方法を学びます。 –

+0

関連のないメモでは、 'std :: string'クラスは' == '演算子(および他の比較演算子)をオーバーロードしました。 'head-> song == song_to_remove'を実行して、2つの文字列が等しいかどうかを調べます。 –

+0

本当ですか?私は文字列に.compare()を使用するように言われていました。 – Justin

答えて

0

私はそれを得ました。私はptrを削除した後にbreakステートメントを持っていなかったので、ptrに何かを割り当てようとしていました。同じ名前の曲が複数ある場合はすべて削除しますが、プログラムはそのようなものである必要はありません。今

node* remove(node *head) 
{ 
    if (head == nullptr) 
    { 
     cout << "There are no songs." << endl; 
     return head; 
    } 

    string song_to_remove; 
    bool found = false; 

    cout << "Enter song name to 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; 
} 
+0

これが問題の解決策である場合は、この回答を正しい解決策としてマークして、同様の問題のある将来の読者を助けることを検討してください。 – bracco23

関連する問題