|
|
#2
lemonandtree2017-12-15 21:31
|
程序代码:#include<iostream>
using namespace std;
#define null 0
struct stu*creat();
void h(char s[20],struct stu*p,struct stu*q);//定义h函数来删除结点
struct stu
{
int num;
char name[10];
char sex[5];
int age;
char major[20];
struct stu*next;
};
void main()
{
struct stu*head,*p,*q;
struct stu a={1,"mike","m",17,"a"};
struct stu b={2,"bob","m",18,"finance"};
struct stu c={3,"sam","f",17,"art"};
struct stu d={4,"jack","m",19,"art"};
struct stu e={5,"jane","f",18,"accountant"};
a.next=&b;b.next=&c;c.next=&d;d.next=&e;e.next=null;//构建简单链表
int n,i;
char s[20];
scanf("%s",s);
head=&a;
q=head;//设置p,q分别指向第一第二个结点
p=&b;
if(strcmp(s,q->major)==0)
head=&b;
else
h(s,p,q);
cout<<"*******************"<<endl;
while(head!=null)
{
cout<<head->major<<endl;//输出专业检验是否成功
head=head->next;
}
}
void h(char s[20],struct stu*p,struct stu*q)
{
do{
if(strcmp(s,p->major)==0)
{
q->next=p->next;//把p所指向的结点删去
p=p->next;//p指向下一个结点
continue;//直接进行下一次循环,从而删除多个专业为s的结点
}
p=p->next;//如果此时的结点中专业不为s,则q,p结点都向后移动一位
q=q->next;
}while(p!=null);
}
using namespace std;
#define null 0
struct stu*creat();
void h(char s[20],struct stu*p,struct stu*q);//定义h函数来删除结点
struct stu
{
int num;
char name[10];
char sex[5];
int age;
char major[20];
struct stu*next;
};
void main()
{
struct stu*head,*p,*q;
struct stu a={1,"mike","m",17,"a"};
struct stu b={2,"bob","m",18,"finance"};
struct stu c={3,"sam","f",17,"art"};
struct stu d={4,"jack","m",19,"art"};
struct stu e={5,"jane","f",18,"accountant"};
a.next=&b;b.next=&c;c.next=&d;d.next=&e;e.next=null;//构建简单链表
int n,i;
char s[20];
scanf("%s",s);
head=&a;
q=head;//设置p,q分别指向第一第二个结点
p=&b;
if(strcmp(s,q->major)==0)
head=&b;
else
h(s,p,q);
cout<<"*******************"<<endl;
while(head!=null)
{
cout<<head->major<<endl;//输出专业检验是否成功
head=head->next;
}
}
void h(char s[20],struct stu*p,struct stu*q)
{
do{
if(strcmp(s,p->major)==0)
{
q->next=p->next;//把p所指向的结点删去
p=p->next;//p指向下一个结点
continue;//直接进行下一次循环,从而删除多个专业为s的结点
}
p=p->next;//如果此时的结点中专业不为s,则q,p结点都向后移动一位
q=q->next;
}while(p!=null);
}