刪除中間節點時出現問題。如果我在中間放置一個位置,所有前面的節點都會消失,任何人都可以幫助我,謝謝!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;
}
作爲一個側面說明,你跳格是不相符的,這不利於 – UKMonkey
請學習不要使用['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