📄 delete.cpp
字号:
#include"head.h"
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
void DeleteStudent(Student* &head)
{
Student* pGuard;
Student* p;
char Name[20];
char choice;
int Class;
int Number;
pGuard=head;
cout<<"说明:"<<endl
<<"本程序提供①按照学生学号"<<endl
<<" ②按照学生所在班级和姓名"<<endl
<<" 两种方式来删除一个学生的信息."
<<endl;
cout<<"请选择删除方式(1/2):";
cin>>choice;
system("cls");
if(choice=='1')
{
status1:
cout<<"请输入要删除的学生学号:";
cin>>Number;
if(head->Number==Number)
{
head=pGuard->next;
delete pGuard;
cout<<"删除成功!"<<endl;
return;
}
for(pGuard=head;pGuard->next!=NULL;pGuard=pGuard->next)
{
if(pGuard->next->Number==Number)
{
p=pGuard->next;
pGuard->next=p->next;
delete p;
cout<<"删除成功!"<<endl;
return;
}
}
status2:
cout<<"没有找到您输入的学生学号,删除失败."<<endl;
cout<<"要重新输入吗?(y/n)";
cin>>choice;
if(choice=='y'||choice=='Y')
{
system("cls");
goto status1;
}
else if(choice!='y'&&choice!='Y'&&choice!='n'&&choice!='N')
{
cout<<"您键入的内容有误,请重新选择."<<endl;
goto status2;
}
}
else if(choice=='2')
{
status3:
cout<<"请输入要删除的学生的班级:"<<endl;
cin>>Class;
cout<<"请输入要删除的学生的姓名:";
cin>>Name;
if(head->Class==Class&&strcmp(head->Name,Name)==0)
{
head=pGuard->next;
delete pGuard;
cout<<"删除成功!"<<endl;
return;
}
for(pGuard=head;pGuard->next!=NULL;pGuard=pGuard->next)
{
if(pGuard->next->Class==Class&&strcmp(pGuard->next->Name,Name)==0)
{
p=pGuard->next;
pGuard->next=p->next;
delete p;
cout<<"删除成功!"<<endl;
return;
}
}
status4:
cout<<"没有找到您输入的学生,删除失败."<<endl;
cout<<"要重新输入吗?(y/n)";
cin>>choice;
if(choice=='y'||choice=='Y')
{
system("cls");
goto status3;
}
else if(choice!='y'&&choice!='Y'&&choice!='n'&&choice!='N')
{
cout<<"您键入的内容有误,请重新选择."<<endl;
goto status4;
}
}
}
void DeleteClass(Student* &head)
{
Student* left;
Student* right;
Student* p;
char choice;
int Class;
left=head;
status5:
cout<<"请输入您要删除的班级:";
cin>>Class;
if(head->Class==Class)
{
for(right=head;right->next!=NULL;right=right->next)
{
if(right->next->Class!=Class)
{
break;
}
}
head=right->next; //改变head的指向
while(left!=right) //以下一段删除left和right之间的所有节点
{
p=left;
left=left->next;
delete p;
}
delete left; //这里因为left=right,也可以用 "delete right;"
cout<<"删除成功!"<<endl;
return;
}
for(left=head;left->next!=NULL;left=left->next) //左指针定位
{
if(left->next->Class==Class) //there used to be an error here!!!!
{
break;
}
}
if(left->next==NULL) //如果左指针没有定位,那么右指针也不用定位了
{
status6:
cout<<"没有找到您输入的班级,删除失败."<<endl;
cout<<"要重新输入吗?(y/n):";
cin>>choice;
if(choice=='y'||choice=='Y')
{
system("cls");
goto status5;
}
else if(choice!='y'&&choice!='Y'&&choice!='n'&&choice!='N')
{
cout<<"您键入的内容有误,请重新选择."<<endl;
goto status6;
}
}
for(right=left;right->next!=NULL;right=right->next) //右指针定位
{
if(right->next->Class!=Class)
{
break;
}
}
for(p=head;p->next !=left;p=p->next) //改变left前一节点中next的指向
;
p->next=right->next;
while(left!=right) //和上面一样,删除left和right之间的所有节点
{
p=left;
left=left->next;
delete p;
}
delete left;
cout<<"删除成功!"<<endl;
}
void DeleteAll(Student* &head) //释放动态分配的内存
{
Student *pGuard;
Student *p;
p=pGuard=head;
while(p!=NULL)
{
pGuard=pGuard->next;
delete p;
p=pGuard;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -