#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct node
{
char num[20];
char name[20];
char sex[5];
char major[20];
char phone[20];
node *next;
}*List;
List creat(List head,node x)
{
if(head == NULL)
{
head=(List)malloc(sizeof(node));
strcpy(head->num,x.num);
strcpy(head->name,x.name);
strcpy(head->sex,x.sex);
strcpy(head->major,x.major);
strcpy(head->phone,x.phone);
head->next=NULL;
}
else
{
List p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=(List)malloc(sizeof(node));
strcpy(p->next->num,x.num);
strcpy(p->next->name,x.name);
strcpy(p->next->sex,x.sex);
strcpy(p->next->major,x.major);
strcpy(p->next->phone,x.phone);
p->next->next=NULL;
}
return head; //
}
List insert(List p,char name[],node y)
{
List n;
if(p->next!=NULL)
{
n=(List)malloc(sizeof(node));
p->next=n;
strcpy(p->next->num,y.num);
strcpy(p->next->name,y.name);
strcpy(p->next->sex,y.sex);
strcpy(p->next->major,y.major);
strcpy(p->next->phone,y.phone);
}
else
{
n=(List)malloc(sizeof(node));
p->next=n;
strcpy(p->next->num,y.num);
strcpy(p->next->name,y.name);
strcpy(p->next->sex,y.sex);
strcpy(p->next->major,y.major);
strcpy(p->next->phone,y.phone);
n->next=NULL;
}
return p->next;
}
List serac(List head,char num[])
{
List p=head;
int flag=0;
while(p->next!=NULL)
{
if (strcmp(p->num,num)==0)
{
printf("%s\t%s\t%s\t%s\t%s\n",p->num,p->name,p->sex,p->major,p->phone);
flag=1;
}
p=p->next;
}
if(!flag) printf("NO!!\n");
return head;
}
List del(List head,char name[])
{
List p=head;
List temp;
while(p->next!=NULL)
{
if(strcmp(head->name,name)==0)
{
p=head->next;
free(head);
head=NULL;
return p;
}
if(strcmp(p->next->name,name)==0)
{
temp=p->next;
p->next=p->next->next;
free(temp);
temp=NULL;
return head;
}
p=p->next;
}
p=NULL;
return head;
}
void print(List head)
{
List p=head;
while (p!=NULL)
{
printf("%s\t%s\t%s\t%s\t%s\n",p->num,p->name,p->sex,p->major,p->phone);
p=p->next;
}
}
int main()
{
node x;
List head=NULL;
while (scanf("%s%s%s%s%s",x.num,x.name,x.sex,x.major,x.phone))
{
if(strcmp(x.num,"#")==0) break;
head=creat(head,x);
}
print(head);
char num[20];
char name1[10];
//查询信息
printf("查询信息:\n");
scanf("%s",num);
serac(head,num);
//插入信息,多组信息
printf("插入信息到某人姓名后面:");
scanf("%s",name1);
List q=head;
List temp;
while(strcmp(q->name,name1)!=0) q=q->next;
temp=q->next;//原先下一个信息的指针
while (scanf("%s%s%s%s%s",x.num,x.name,x.sex,x.major,x.phone))
{
if(strcmp(x.num,"#")==0) break;
q=insert(q,name1,x);
}
q->next=temp;
print(head);
//删除信息
printf("输入删除人的姓名:\n");
scanf("%s",name1);
head=del(head,name1);
print(head);
return 0;
}
链表基本操作(升级版)
最新推荐文章于 2023-06-28 15:04:19 发布