?? 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之間的所有節(jié)點
{
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前一節(jié)點中next的指向
;
p->next=right->next;
while(left!=right) //和上面一樣,刪除left和right之間的所有節(jié)點
{
p=left;
left=left->next;
delete p;
}
delete left;
cout<<"刪除成功!"<<endl;
}
void DeleteAll(Student* &head) //釋放動態(tài)分配的內存
{
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 + -