64,637
社区成员
发帖
与我相关
我的任务
分享
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
typedef struct ListNode
{
string sID;
string sName;
string bSex;
double Score[2];
ListNode* pNext;
}Node;
class StudentList
{
Node* pHead;
Node* pNew;
Node* pTail;
public:
StudentList(){pHead=NULL;};
Node* ReadStudent(StudentList &list);
void DeleteStudent();
void ShowStudent(StudentList &list);
Node* SelectStudent();
Node* SearchStudent();
};
void StudentList::ShowStudent(StudentList &list)
{
cout<<"ID "<<"Name "<<"Sex "<<"English "<<"Math"<<endl;
Node* p=pHead;
while(p!=NULL)
{
cout<<p->sID<<" "<<p->sName<<" "<<p->bSex<<" "
<<p->Score[0]<<" "<<p->Score[1]<<endl;
p=p->pNext;
}
}
void StudentList::DeleteStudent()
{
string ID;
cout<<"Please input the student's ID"<<endl;
cin>>ID;
Node* pPre;
Node* p=pHead;
while(p!=NULL)
{
pPre=p;
if(ID==p->sID)//因为学号唯一,所以查找到后直接删除,不用继续查找
{
pPre->pNext=p->pNext;
delete p;
cout<<"删除成功!"<<endl;
return;
}
p=p->pNext;
}
cout<<"不存在些学号的学生纪录"<<endl;
}
while(p!=NULL)
{
pPre=p;
if(ID==p->sID)//因为学号唯一,所以查找到后直接删除,不用继续查找
{
pPre->pNext=p->pNext; // 这里明显有问题。
delete p;
cout<<"删除成功!"<<endl;
return;
}
p=p->pNext;
}
/****修改后***/
while(p->next!=NULL) //因为你是单链,删除后,找不到前驱节点。
{
pPre=p->next; //pPre标记要删除的对象
if(ID==p->next->sID)//唯一,所以查找到后直接删除,不用继续查找
{
p->pNext=p->pNext->next; //要删除pPre,p指向它
delete pPre;
cout<<"删除成功!"<<endl;
return;
}
p=p->pNext;
}