创建函数
struct student *creat() {
struct student*head;
struct student*p1,*p2;
p1=p2=(struct student*)malloc(LEN);//开创空间
printf("输入num\n");
scanf("%d",&p1->num);
printf("输入score\n");
scanf("%f",&p1->score);
head=NULL;
n=0;
while(p1->num!=0) {//num==0是结束标志
n++;
if(n==1) {
head=p1;
} else {
p2->next=p1;
}
p2=p1;
p1=(struct student*)malloc(LEN);
printf("输入num\n");
scanf("%d",&p1->num);
printf("输入score\n");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
struct student *del(struct student **head,int n,int number) {
struct student*p1,*p2;
if(head==NULL) {
printf("this is null list\n");
return NULL;//判断是不是空链表
}
p1=(*head);
while(p1->num!=n && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}//寻找到删除的num
if(p1->num==n)
{
if(p1==(*head))
{
free(*head);//释放head
(*head)=p1->next;
}
else
{
p2->next=p1->next;
}
printf("Delete %d success\n",n);
number=number-1;
printf("删除后学生数量为%d\n",number);
}
else
{
printf("%d not fuond\n",n);
}
return (*head);
}
//delete头节点需要用双指针,类似传入改变全局变量的方法,否则返回之后仍然和原来链表一样
插入段
struct student *insert(struct student *head,struct student *stu_2) {
if(stu_2->num==0)//仍然用0作为结束符号
return head;
struct student*p0,*p1,*p2;
p0=stu_2;
p1=head;
if(head==NULL) {
head=p0;
p0->next=NULL;
} else {
while((p0->num>p1->num)&&(p1->next!=NULL)) {
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num) {
if(p1==head) {
head=p0;
} else {
p2->next=p0;
}
p0->next=p1;
} else {
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
完整代码
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();//创建链表函数
struct student *del(struct student *head,int num);
struct student *insert(struct student *head,struct student *stu_2);
void print(struct student **head);
struct student {
int num;
float score;
struct student *next;
};
int n;//记录存放节点数
struct student *creat() {
struct student*head;
struct student*p1,*p2;
//struct student*p2;
p1=p2=(struct student*)malloc(LEN);
printf("输入num\n");
scanf("%d",&p1->num);
printf("输入score\n");
scanf("%f",&p1->score);
head=NULL;
n=0;
while(p1->num!=0) {
n++;
if(n==1) {
head=p1;
} else {
p2->next=p1;
}
p2=p1;
p1=(struct student*)malloc(LEN);
printf("输入num\n");
scanf("%d",&p1->num);
printf("输入score\n");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head) {
struct student *p;
p=head;
if(head!=NULL) {
do {
printf("num is %d,score is %f\n",p->num,p->score);
p=p->next;
} while(p);
}
}
struct student *del(struct student **head,int n,int number) {
struct student*p1,*p2;
if(head==NULL) {
printf("this is null list\n");
return NULL;
}
p1=(*head);
while(p1->num!=n && p1->next!=NULL) {
p2=p1;
p1=p1->next;
}
if(p1->num==n) {
if(p1==(*head)) {
free(*head);
(*head)=p1->next;
} else {
p2->next=p1->next;
}
printf("Delete %d success\n",n);
number=number-1;
printf("删除后学生数量为%d\n",number);
} else {
printf("%d not fuond\n",n);
}
return (*head);
}
struct student *insert(struct student *head,struct student *stu_2) {
if(stu_2->num==0)
return head;
struct student*p0,*p1,*p2;
p0=stu_2;
p1=head;
if(head==NULL) {
head=p0;
p0->next=NULL;
} else {
while((p0->num>p1->num)&&(p1->next!=NULL)) {
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num) {
if(p1==head) {
head=p0;
} else {
p2->next=p0;
}
p0->next=p1;
} else {
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
int main(void) {
struct student*stu,*p,stu_2;
int i;
stu=creat();
print(stu);
printf("\n");
printf("\n");
printf("学生数量为%d\n",n);
printf("请输入删除\n");
scanf("%d",&i);
del(&stu,i,n);
print(stu);
printf("\n");
printf("\n");
printf("insert num\n");
scanf("%d",&stu_2.num);
printf("insert score\n");
scanf("%f",&stu_2.score);
stu=insert(stu,&stu_2);
print(stu);
printf("\n");
}