删除节点:
先考虑删除中间的节点情况
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student *creat(void);
struct student *p=creat();
print(p);
int number;
printf("请输入需要删除的学号:");
scanf("%d",&number);
del(p,number);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
void del(struct student *header,int number)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num==number)
{
p2->next=p1->next;
break;
}
else
{
p2=p1;
p1=p1->next;
}
}
}
增加对第一个节点的删除情况
为什么不对?
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
void del(struct student *header,int number);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
int number;
printf("请输入需要删除的学号:");
scanf("%d",&number);
del(p,number);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
void del(struct student *header,int number)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num==number)
{
if(p1==header)
{
header=p1->next;
break;
}
else
p2->next=p1->next;
break;
}
else
{
p2=p1;
p1=p1->next;
}
}
}
改正:
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student *del(struct student *header,int number);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
int number;
printf("请输入需要删除的学号:");
scanf("%d",&number);
p=del(p,number);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
struct student *del(struct student *header,int number)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num==number)
{
if(p1==header)
{
header=p1->next;
break;
}
else
p2->next=p1->next;
break;
}
else
{
p2=p1;
p1=p1->next;
}
}
return header;
}
考虑没有命中的情况
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student *del(struct student *header,int number);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
int number;
printf("请输入需要删除的学号:");
scanf("%d",&number);
p=del(p,number);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
struct student *del(struct student *header,int number)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num==number)
{
if(p1==header)
{
header=p1->next;
break;
}
else
p2->next=p1->next;
break;
}
else
{
p2=p1;
p1=p1->next;
if(p1==NULL)
break;
}
}
return header;
}
插入节点:
首先考虑在中间插入的情况
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
void insert(struct student *header,struct student *p0);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
//需要链表元素的学号升序排列
struct student * p0=(struct student *)malloc(sizeof(struct student));
printf("请输入要插入的学号和成绩:");
scanf("%ld,%f",&p0->num,&p0->score);
insert(p,p0);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
void insert(struct student *header,struct student *p0)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num<p0->num)
{
p2=p1;
p1=p1->next;
}
else if(p1->num>p0->num)
{
p2->next=p0;
p0->next=p1;
break;
}
}
}
考虑在尾部增加的情况
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
void insert(struct student *header,struct student *p0);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
//需要链表元素的学号升序排列
struct student * p0=(struct student *)malloc(sizeof(struct student));
printf("请输入要插入的学号和成绩:");
scanf("%ld,%f",&p0->num,&p0->score);
insert(p,p0);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
void insert(struct student *header,struct student *p0)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num<p0->num)
{
p2=p1;
p1=p1->next;
if(p1==NULL)
{
p2->next=p0;
p0->next=NULL;
break;
}
}
else if(p1->num>p0->num)
{
p2->next=p0;
p0->next=p1;
break;
}
}
}
考虑在第一个节点前插入
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student * insert(struct student *header,struct student *p0);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
//需要链表元素的学号升序排列
struct student * p0=(struct student *)malloc(sizeof(struct student));
printf("请输入要插入的学号和成绩:");
scanf("%ld,%f",&p0->num,&p0->score);
p=insert(p,p0);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
struct student * insert(struct student *header,struct student *p0)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num<p0->num)
{
p2=p1;
p1=p1->next;
if(p1==NULL)
{
p2->next=p0;
p0->next=NULL;
break;
}
}
else if(p1->num>p0->num)
{
if(header==p1)
{
header=p0;
p0->next=p1;
break;
}
else
{
p2->next=p0;
p0->next=p1;
break;
}
}
}
return header;
}
考虑等于的情况
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student * insert(struct student *header,struct student *p0);
void print(struct student *p);
struct student *creat(void);
struct student *p=creat();
print(p);
//需要链表元素的学号升序排列
struct student * p0=(struct student *)malloc(sizeof(struct student));
printf("请输入要插入的学号和成绩:");
scanf("%ld,%f",&p0->num,&p0->score);
p=insert(p,p0);
print(p);
}
void print(struct student *p)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc (LEN);
scanf("%ld,%f",&p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
struct student * insert(struct student *header,struct student *p0)
{
struct student *p1,*p2;
p1=header;
p2=header;
while(1)
{
if(p1->num<p0->num)
{
p2=p1;
p1=p1->next;
if(p1==NULL)
{
p2->next=p0;
p0->next=NULL;
break;
}
}
else if(p1->num>p0->num)
{
if(header==p1)
{
header=p0;
p0->next=p1;
break;
}
else
{
p2->next=p0;
p0->next=p1;
break;
}
}
else
{
break;
}
}
return header;
}