/*建立一个链表,每个结点包括学号,姓名,性别,年龄*/
#include <stdio.h>
#include <stdlib.h>
typedef struct information info;
typedef struct information{
int num;
char name[10];
char sex[5];
int age;
info *next;
}info;
info *Createlist();
info *Deletelist(info *head,int new_age);
int main(){
info *head=NULL,*p;
head=Createlist();
int new_age;
scanf("%d",&new_age);
head=Deletelist(head,new_age);
for(p=head;p!=NULL;p=p->next){
printf("%d\t%s\t%s\t%d\n",p->num ,p->name ,p->sex ,p->age );
}
return 0;
}
info *Createlist(){
info *head=NULL,*tail=NULL,*p;
int x;
scanf("%d",&x);
while(x!=0){
p=(info *)malloc(sizeof(info));
p->num =x;
scanf("%s%s%d",p->name ,p->sex ,&p->age );
p->next=NULL;
if(head==NULL){
head=p;
} else{
tail->next=p;
}
tail=p;
scanf("%d",&x);
}
printf("OK!\n");
return head;
}
info *Deletelist(info *head,int new_age){
info *ptr1,*ptr2;
while(head!=NULL&&head->age==new_age){
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL){
return NULL;
}
ptr1=head;
ptr2=head->next ;
while(ptr2!=NULL){
if(ptr2->age ==new_age){
ptr1->next =ptr2->next ;//ptr1链接ptr2后一个结点
free(ptr2);//删除ptr2所指结点 :不对内容作任何修改,只是标记这块区域,告诉系统可以重新分配
}else{
ptr1=ptr2;
}
ptr2=ptr2->next;//ptr2后移一位
}
return head;
}
/*建立一个链表,每个结点包括学号,姓名,性别,年龄*/
最新推荐文章于 2023-01-04 00:41:45 发布