代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct aaa *creat_jiedian(void); //创建节点
struct aaa *creat_lianbiao(int len); //连接节点形成完整的链表
struct aaa *find_jiedian(int id,struct aaa *head);
struct aaa *delete_jiedian(int id,struct aaa *head);
int delete2_jiedian(int id,struct aaa *head);
struct aaa * delete3_jiedian(int id,struct aaa *head);
struct aaa
{
/*数据域*/
char name[10];
char sex;
int id;
float score;
/*指针域*/
struct aaa *pnext;
};
int main()
{
int len,id;
struct aaa *head=NULL,*cha;
printf("请输入添加人数\n");
scanf("%d",&len);
head = creat_lianbiao(len);
/*
printf("请输入所查询的ID\n");
scanf("%d",&id);
*/
cha = head;
while(cha != NULL){
printf("名字:%s ID:%d 成绩:%.2f\n",cha->name,cha->id,cha->score);
cha=cha->pnext;
}
/*
while(head!=NULL)
{
printf("名字:%s 性别:%c 年龄:%d 成绩:%.2f\n",head->name,head->sex,head->id,head->score);
head=head->pnext;
}
*/
scanf("%d",&id);
delete2_jiedian(id,head);
cha = head;
/*
scanf("%d",&id);
head=delete3_jiedian(id,head);
cha = head;
*/
while(cha != NULL){
printf("名字:%s ID:%d 成绩:%.2f\n",cha->name,cha->id,cha->score);
cha=cha->pnext;
}
}
struct aaa *creat_jiedian(void)
{
struct aaa *m,*p;
m = (struct aaa *)malloc(sizeof(struct aaa));
if(m==NULL)
{
return NULL;
}
else
{
m->pnext=NULL;
}
return m;
}
struct aaa *creat_lianbiao(int len)
{
int i;
struct aaa *head=NULL,*pf,*fb; //head表头 pf接收每次创建的节点地址 fb穿针引线
for(i=0;i<len;i++)
{
pf = creat_jiedian();
printf("请输入学号,姓名,成绩:");
scanf("%d %s %f",&pf->id,pf->name,&pf->score);
if(i==0)
{
head = pf;
}
else
{
fb->pnext = pf;
}
fb = pf;
}
return head;
}
struct aaa *find_jiedian(int id,struct aaa *head)
{
struct aaa *find = head; //使find指向第一个节点
while(find!=NULL)
{
if(find->id==id)
{
return find;
}
else
{
find = find->pnext;
}
}
if(find == NULL)
{
return NULL;
}
}
struct aaa *delete_jiedian(int id,struct aaa *head)
{
struct aaa *delete = head; //使find指向第一个节点
struct aaa *fine = delete->pnext;
if(delete == NULL)//假头
{
return NULL;
}
while(delete!=NULL)
{
if(delete->id==id)
{
fine->pnext = delete->pnext;
free(delete);
return head;
}
else
{
delete = delete->pnext;
fine = delete->pnext;
}
}
if(delete == NULL)//没有ID
{
return head;
}
}
int delete2_jiedian(int id,struct aaa *head)
{
struct aaa *find = head;
struct aaa *delete = NULL;
if(find == NULL)//假头
{
printf("你输入的数据链不存在\n");
return 0;
}
while(find!=NULL)
{
if(find->id==id)
{
delete= find;
break;
}
else
{
find = find->pnext;
}
}
if(delete == NULL)//没有ID
{
printf("你输入的ID不存在\n");
return 0;
}
find = head;
while(1)
{
if(delete==find)
{
*head=*(find->pnext);
memset(find,0,sizeof(struct aaa));
free(delete);
return 1;
}
else if(find->pnext==delete)
{
find->pnext=delete->pnext;
free(delete);
return 1;
}
else
{
find = find->pnext;
}
}
}
struct aaa * delete3_jiedian(int id,struct aaa *head)
{
struct aaa *find = head;
struct aaa *delete = NULL;
if(find == NULL)//假头
{
printf("你输入的数据链不存在\n");
return head;
}
while(find!=NULL)
{
if(find->id==id)
{
delete= find;
break;
}
else
{
find = find->pnext;
}
}
if(delete == NULL)//没有ID
{
printf("你输入的ID不存在\n");
return head;
}
find = head;
while(1)
{
if(delete==find)
{
head=find->pnext;
free(delete);
return head;
}
else if(find->pnext==delete)
{
find->pnext=delete->pnext;
free(delete);
return head;
}
else
{
find = find->pnext;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。