#include<stdio.h>
#include<malloc.h>
typedef struct StudentType
{
int age;
char name[20];
struct StudentType *next;
}*Linklist,Link;
Linklist Build(int n);
Linklist Delete(Linklist head,int n,int m);
void Print(Linklist head);
int main()
{
Linklist head=NULL,first=NULL;
int n,m;
printf("请输入要输入的总人数:\n");
scanf("%d",&n);
head=Build(n);
Print(head);
printf("请输入要删除的年龄:\n");
scanf("%d",&m);
first=Delete(head,n,m);
Print(first);
return 0;
}
Linklist Build(int n)
{
int i;
Linklist first=NULL,head=NULL,p=NULL;
head=(Linklist)malloc(sizeof(Link));
head->next=NULL;
p=head;
scanf("%d%s",&p->age,p->name);
if(n>1)
{
for(i=1;i<n;i++)
{
first=(Linklist)malloc(sizeof(Link));
scanf("%d%s",&first->age,first->name);
first->next=p;
p=first;
}
}
return p;
}
Linklist Delete(Linklist head,int n,int m)
{
int count=0;
int flag=0;
Linklist ptr=head,first=NULL,p=NULL;
if(ptr->age==m)
{
while(ptr->age==m&&ptr->next!=NULL)
{
count++;
flag=1;
p=ptr;
ptr=ptr->next;
head=ptr;
free(p);
}
if(ptr->next==NULL&&ptr->age==m)
{
count++;
flag=1;
free(ptr);
head=NULL;
}
}
else
{
ptr=head->next;
first=head;
while(ptr->next!=NULL)
{
if(ptr->age==m)
{
p=ptr;
first->next=ptr->next;
free(p);
ptr=ptr->next;
flag=1;
count++;
}
else
{
ptr=ptr->next;
first=first->next;
}
}
if(ptr->age==m)
{
flag=1;
first->next=NULL;
count++;
}
}
if(count==n) head=NULL;
if(flag==1)
printf("delete successfully!\n");
else printf("not found\n");
return head;
}
void Print(Linklist head)
{
Linklist p=head;
if(head==NULL) printf("无数据\n");
else
{
while(p!=NULL)
{
printf("%d\n%s\n",p->age,p->name);
p=p->next;
}
}
}
#include<malloc.h>
typedef struct StudentType
{
int age;
char name[20];
struct StudentType *next;
}*Linklist,Link;
Linklist Build(int n);
Linklist Delete(Linklist head,int n,int m);
void Print(Linklist head);
int main()
{
Linklist head=NULL,first=NULL;
int n,m;
printf("请输入要输入的总人数:\n");
scanf("%d",&n);
head=Build(n);
Print(head);
printf("请输入要删除的年龄:\n");
scanf("%d",&m);
first=Delete(head,n,m);
Print(first);
return 0;
}
Linklist Build(int n)
{
int i;
Linklist first=NULL,head=NULL,p=NULL;
head=(Linklist)malloc(sizeof(Link));
head->next=NULL;
p=head;
scanf("%d%s",&p->age,p->name);
if(n>1)
{
for(i=1;i<n;i++)
{
first=(Linklist)malloc(sizeof(Link));
scanf("%d%s",&first->age,first->name);
first->next=p;
p=first;
}
}
return p;
}
Linklist Delete(Linklist head,int n,int m)
{
int count=0;
int flag=0;
Linklist ptr=head,first=NULL,p=NULL;
if(ptr->age==m)
{
while(ptr->age==m&&ptr->next!=NULL)
{
count++;
flag=1;
p=ptr;
ptr=ptr->next;
head=ptr;
free(p);
}
if(ptr->next==NULL&&ptr->age==m)
{
count++;
flag=1;
free(ptr);
head=NULL;
}
}
else
{
ptr=head->next;
first=head;
while(ptr->next!=NULL)
{
if(ptr->age==m)
{
p=ptr;
first->next=ptr->next;
free(p);
ptr=ptr->next;
flag=1;
count++;
}
else
{
ptr=ptr->next;
first=first->next;
}
}
if(ptr->age==m)
{
flag=1;
first->next=NULL;
count++;
}
}
if(count==n) head=NULL;
if(flag==1)
printf("delete successfully!\n");
else printf("not found\n");
return head;
}
void Print(Linklist head)
{
Linklist p=head;
if(head==NULL) printf("无数据\n");
else
{
while(p!=NULL)
{
printf("%d\n%s\n",p->age,p->name);
p=p->next;
}
}
}