#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
long score;
struct student *next;
};
int j,listnum,delnum,n;
int num1=1000,score1=2000;
struct student *head,*p1,*p2;
/*输出链表*/
void print(){
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n,p1=p1->next){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
}
}
/*删除一个节点*/
void deletelist(){
p1=head;
n=1;
do{
printf("/ndelete:put in the right order:from %d to %d/n",1,listnum);
scanf("%d",&j);
}while(j<1||j>listnum);/*从 1到listnum 有效*/
while(n<j){/*查找第j个元素,当n等于j时,跳出循环*/
p2=p1;p1=p1->next;++n;
}
if(j==1){
head=p1->next;
printf("the num:%ld,the score:%ld/n",p1->num,p1->score);
}
else{
p2->next=p1->next;
printf("/nyou have delete : num:%ld, score:%ld/n",p1->num,p1->score);
free(*p1);
}
--listnum;/*删除节点结束*/
}
/*插入一个节点*/
void insertlist(){
do{
printf("/n insert: put in the right order: from %d to %d/n",1,listnum+1);
scanf("%d",&j);
}while(j<1||j>listnum+1);/*从 1到listnum+1 有效*/
p1=head;
n=1;
while(n<j){p2=p1;p1=p1->next;n++;}
p1=(struct student*)malloc(LEN);
p1->num=num1++;p1->score=score1++;
if(j==1)
{p1->next=head;head=p1;}
else
{p1->next=p2->next;p2->next=p1;}
++listnum;/*插入节点结束*/
}
main(){
/*生成链表。。。*/
printf("/nput in the length of the list: ");
scanf("%d",&listnum);
for(n=1;n<=listnum;n++){
p1=(struct student*)malloc(LEN);
p1->num=num1++; p1->score=score1++ ;
if(n==1) head=p2=p1;
else {p2->next=p1;p2=p1;}
}
p2->next=NULL;
printf("head: %0x,last:%0x/n",head,p1);/*链表生成结束*/
print();
deletelist();
print();
insertlist();
print();
scanf("%d",&j);
}