|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int number;
char name[10];
struct student * next;
};
//主函数
void main()
{ void print(struct student * head);//声明
struct student * shanchu(struct student *p3,char k[10]);
struct student * dig(struct student *p3,char m[10],int n,int u);
struct student * creat(void);
struct student * p3,* head1;
int h,n,u;
char k[10];
char m[10];
printf("你想插入哪个人,人名,学号,请输入1,你想删除哪个人,请输入2\n");
scanf("%d",&h);
if(h==2)
{
scanf("%s",&k);
p3=creat();
head1=shanchu(p3,k);
print(head1);
}
else
{
printf("插入人的姓名及学号,某人前面");
scanf("%s %d %d\n",m,&n,&u);
p3=creat();
head1=dig(p3,m,n,u);
printf("123456");
print(head1);
}
}
//创建链表
struct student * creat(void)
{
struct student * p1,*p2;
struct student * head;
FILE * fp;
int n=0;
if((fp=fopen("name2.txt","r"))==NULL)
{
printf("cannot open");
exit(0);
}
p1=p2=(struct student *)malloc(LEN);
fscanf(fp,"%d %s\n",&p1->number,&p1->name);
while(n<30)
{
n=n+1;
if(n>1)
{
p2->next=p1;
p2=p1;
}
else head=p1;
p1=(struct student * )malloc(LEN);
fscanf(fp,"%d %s\n",&p1->number,&p1->name);
}
p1=NULL;
fclose(fp);
return(head);
}
//输出
void print(struct student * head)
{
struct student * p;
p=head;
if(head!=NULL)
{
do
{printf("%d %s\n",p->number,p->name);
p=p->next;}
while(p!=NULL);
}
}
//删除
struct student * shanchu(struct student * p3,char k[])
{
struct student *p5;
struct student * p6,*head2;
p5=p3;
head2=p3;
while(strcmp(k,p5->name)!=0)
{ p6=p5;
p5=p5->next;
}
if(p5==head2)
{
head2=p5->next;
}
else if(p5->next==NULL)
{
p6->next=NULL;
}
else
{
p6->next=p5->next;
}
return(head2);
}
//插入
struct student * dig(struct student * p3,char m[],int n,int u)
{
struct student * head3,*p7,*p8,*p0;
int i=0;
struct student p;
p0=(struct student *)malloc(LEN);
strcpy((p0->name),m);
p0->number=n;
p8=p3;
while(u!=p8->number)
{
p7=p8;
p8=p8->next;
}
p7->next=p0;
p0->next=p8;
return(p3);
} |
|