用C语言实现线性表的基本操作,能创建一个基于学生信息管理的链表

2024-11-24 00:44:43
推荐回答(1个)
回答1:

已经调通的一个程序,你细微修改一下就好了
/*C语言封笔*/
#include
#include
#define NULL 0
#define dd sizeof(struct stu)
struct stu{
int num;
char name[10];
struct stu *next;
};
int n=0;

struct stu *create(void)/*创建*/
{struct stu *p1,*p2,*head=NULL;
p1=p2=(struct stu *)malloc(dd);
printf("input num,name then enter\n");
scanf("%d,%s",&p1->num,&p1->name);
head=p1;
for(n=0;n<3;n++)
{p2->next=p1;
p2=p1;
if(n==2);
else
{p1=(struct stu *)malloc(dd);
printf("input num,name then enter\n");
scanf("%d,%s",&p1->num,&p1->name);
}
}
p2->next=NULL;
return(head);
}

void output(struct stu *head)/*输出*/
{struct stu *p;
p=head;
while(p!=NULL)
{n++;
printf("%5d,%10s\n",p->num,p->name);
p=p->next;
}
}

/*插入*/
struct stu *charu(struct stu *head)
{struct stu *p1,*p2,*p3;
p2=(struct stu *)malloc(dd);
printf("input a point,num,name");
scanf("%d,%s",&p2->num,&p2->name);
p1=head;
n=0;
if (p2->numnum)
{head=p2;head->next=p1;}
else
{for(n=1;n<3;n++)
{p3=p1;
p1=p1->next;
if (p2->numnum)
{p3->next=p2;
p2->next=p1;
}
}
}
if(p1->numnum)
{p1->next=p2;
p2->next=NULL;
}
return(head);
}
/*删除*/
struct stu *delect(struct stu *head)
{struct stu *p1,*p2;
int m;
printf("input a num, then delete\n");
scanf("%d",&m);
p1=head;
p2=p1->next;
n=0;
if (m==p1->num)
head=p2;
else
{for(n=1;n<=3;n++)
{if (m==p2->num)
p1->next=p2->next;
p1=p2;
p2=p2->next;
}
}
return(head);
}

/*修改*/
struct stu *xiugai(struct stu *head)
{struct stu *p1;
int m;
printf("input the num, then xiugai\n");
scanf("%d",&m);
p1=head;
for(n=0;n<3;n++)
{if (m==p1->num)
{printf("xiu gai kai shi,input num,name\n");
scanf("%d,%s",&p1->num,&p1->name);
break;}
p1=p1->next;
}
return(head);
}
void main()
{struct stu *create(void);
struct stu *xiugai(struct stu *head);
struct stu *delect(struct stu *head);
struct stu *charu(struct stu *head);
void output(struct stu *head);
struct stu *head;int m;
head=create();
output(head);
printf("scan 1 to charu,2 to delet,3 to xiugai,please");
scanf("%d",&m);
switch(m)
{
case 1:head=charu(head);output(head);break;
case 2:head=delect(head);output(head);break;
case 3:head=xiugai(head);output(head);break;
}

}