2016-10-07 6 views
0

途中でノードを削除するときに問題があります。真ん中にポジションを置くと、前のノードはすべて消えてしまいます。ありがとうございました。LinkList C++ノードを削除する

フロントのものを削除しても問題ありませんが、途中で問題があります。

今すぐ停止しました。

#include<iostream> 
#include<string> 
#include <limits> 
using namespace std; 
struct Student{ 
    string name; 
    int matricNo; 
    string course; 
    double cgpa; 
    Student* link; 
}; 

int main(){ 
    Student *head = NULL, *last, *newStudent, *target; 
    int menu = 0; 
    int select; 
    while(menu != 6){ 
    cout << "Student Database.\n"; 
    cout << "1.Add a student.\n"; 
    cout << "2.Delete a student.\n"; 
    cout << "3.View a student's information.\n"; 
    cout << "4.View all students' information.\n"; 
    cout << "5.View all students' information with CGPA of 3.0 or higher.\n"; 
    cout << "6.End program.\n"; 
    while(!(cin >> menu)){ 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << "Invalid input.\n"; 
    } 
    if(menu == 1){ 
    newStudent = new Student; 

    if(head == NULL) 
     head = newStudent; 

     cin.clear(); 
     cin.ignore(2000,'\n'); 
     cout << "Please enter the student's name : "; 
     getline(cin, newStudent -> name); 
     cin.clear(); 
     cout << "Please enter the Matric Number  : "; 
      while(!(cin >> newStudent -> matricNo)){ 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      cout << "Invalid input. Please enter a number.\n"; 
     } 
     cin.clear(); 
     cin.ignore(2000,'\n'); 
     cout << "Please enter the Course : "; 
     getline(cin, newStudent -> course); 
     cin.clear(); 
     cout << "Please enter the student's CGPA : "; 
      while(!(cin >> newStudent -> cgpa) || newStudent -> cgpa > 4 || newStudent -> cgpa < 0){ 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      cout << "Invalid input. Please enter a value between 0.00 and 4.00\n"; 
     } 
     newStudent -> link = NULL; 

    if(last != NULL) 
     last -> link = newStudent; 

    last = newStudent; 
    system("cls"); 
    } 

    if(menu == 2){ 
     if(head != NULL){ 
     cout << "Please enter the matric number of a student : "; 
     while(!(cin >> select)){ 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << "Invalid input.\n"; 
     } 
      for(Student* p = head; p != NULL; p = p -> link){ 
       if(p -> matricNo == select){ 
        target = p; 
        if(head != NULL) 
        head = p -> link; 
        target -> link = NULL; 
        delete target; 
       } 
      } 
     } 
     else if(head == last){ 
      head -> link=NULL; 
      last -> link=NULL; 
     } 
     else 
      cout << "No students in the database.\n"; 
    } 
    if(menu == 3){ 

     if(head != NULL){ 
     cout << "Please enter the matric number of a student : "; 
     while(!(cin >> select)){ 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     cout << "Invalid input.\n"; 
     } 
      for(Student* p = head; p != NULL; p = p -> link){ 
       if(p -> matricNo == select){ 
       cout << "Student's Name : " << p -> name << endl; 
       cout << "Matric Number : " << p -> matricNo << endl; 
       cout << "Course   : " << p -> course << endl; 
       cout << "CGPA  : " << p -> cgpa << endl; 
       cout << "==================================\n"; 
       } 
      } 
     } 
     else 
      cout << "No students in the database.\n"; 
    } 
    if(menu == 4){ 
     if(head != NULL){ 
      for(Student* p = head; p != NULL; p = p -> link){ 
      cout << "Student's Name : " << p -> name << endl; 
      cout << "Matric Number : " << p -> matricNo << endl; 
      cout << "Course   : " << p -> course << endl; 
      cout << "CGPA  : " << p -> cgpa << endl; 
      cout << "==================================\n"; 
      } 
     } 
     else 
      cout << "No students in the database.\n"; 
    } 
    if(menu == 5){ 
     if(head != NULL){ 
      for(Student* p = head; p != NULL; p = p -> link){ 
       if(p -> cgpa >=3){ 
       cout << "Student's Name : " << p -> name << endl; 
       cout << "Matric Number : " << p -> matricNo << endl; 
       cout << "Course   : " << p -> course << endl; 
       cout << "CGPA  : " << p -> cgpa << endl; 
       cout << "==================================\n"; 
       } 
      } 
     } 
     else 
      cout << "No students in the database.\n";  
    } 
    if(menu == 6) 
     return 0; 
    } 
    system("PAUSE"); 
    return 0; 
} 
+0

、あなたのタブ移動は助けない、一貫性がありません – UKMonkey

+0

['system(" Pause ")'](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong)や['using namespace std;']を使わないでください。 (http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – amanuel2

答えて

1

あなたが持っているように、シングルリンクリストの途中でノードを削除するには、まず削除したい1に前に、ノードを見つける必要があります。これは、ノードを削除するノードへのリンクを設定する必要があるためです。

 
     +--------+  +--------+  +--------+ 
... --> | Node A | --> | Node B | --> | Node C | --> ... 
     +--------+  +--------+  +--------+ 

今、あなたは「ノードB」を削除したいと言うことができます:

はややグラフィカルに、あなたのリストは次のようになります言うことができます。あなたはこのようなリストを見て確認する必要がありますことを行うには:

 
        /---------------\ 
     +--------+ | +--------+ | +--------+ 
... --> | Node A | -/ | Node B | -+-> | Node C | --> ... 
     +--------+  +--------+  +--------+ 

は今、「ノードAは、」「ノードB」へリンクしていないので、「ノードB」は、任意のより多くのリストには効果的ではありません。 「ノードB」に到達できないため、また「ノードB」の構造を削除するため、「ノードA」と「ノードB」の両方が「ノードC」にリンクしていても問題はありません。 。

削除するノードがリストの最初のノードである場合は特別なケースがあります。また、リスト内のノードが見つからない場合も処理できる必要があります。もちろん


標準std::list(または単結合std::forward_list)を使用してのような他の解決策は、(UKMonkeyによって示されるように)、があります。ですから、これはリンクされたリストの仕組みの基礎を学ぶための練習にすぎないと思います。

また、各ノードには、リスト内の次のノードだけでなく前のノードへのリンクも含まれています。しかし、上記の原則は同じです。

+0

代わりに、二重リンクされたリスト、またはstd :: list – UKMonkey

0

リスト内の既存のノードを見つけて削除する目的で単一リンクされたリストをトラバースする場合、単純なパラダイムシフトがタスクを大幅に簡素化します。

問題のパラダイムシフトは、リスト内の各ノードへのポインタではなく、リスト内の各ノードへのポインタへのポインタを使用することです。あなたがいることを行うと、if文の典型的な厄介な森はすぐに消え、非常に単純な、簡単な、アルゴリズムによって置き換えられます:注意点として

for (Student **p=&head; *p; p=&(*p)->next) 
{ 
    if ((*p)->matricNo == select) 
    { 
     Student *node= *p; 
     *p=node->next; 
     delete node; 
     break; 
    } 
} 
関連する問題