#include <stdio.h>
#include <stdlib.h>
struct Student{
int num;
int score;
struct Student *next;
};
int n;
int main () {
struct Student * creat();
void print(struct Student * head);
struct Student * delect(struct Student * head);
struct Student * insert(struct Student * head);
struct Student *head;
head=creat();
int b;
print(head);
head=delect(head);
print(head);
head=insert(head);
print(head);
}
struct Student * creat(){
struct Student *head,*p1,*p2;
head=NULL;
p1=p2=(struct Student *)malloc(sizeof(struct Student));
printf("请输入学生信息;\n");
scanf("%d",&p1->num);
while(p1->num!=0){
n=n+1;
if(n==1){
head=p1;
}else{
p2->next=p1;
p2=p1;
}
p1=(struct Student *)malloc(sizeof(struct Student));
scanf("%d",&p1->num);
}
p2->next=NULL;
return head;
}
void print(struct Student * head){
struct Student *p1;
p1=head;
while(p1!=NULL){
printf("%d\n",p1->num);
p1=p1->next;
}
}
struct Student * delect(struct Student * head){
struct Student *p1,*p2;
p1=head;
p2=p1;
int w=0,i=1;
printf("请输入要删除的学生:\n");
scanf("%d",&w);
printf("%d!!\n",w);
if(head==NULL){
printf("空表!\n");
}else{
printf("%d,%d,%d\n",p1->num,p2->num,i);
while((i!=w)&&(p1!=NULL)){
p2=p1;
p1=p1->next;
i++;
}
if(i==1){
head=head->next;
printf("删除成功\n");
return head;
}
if(i==w){
p2->next=p1->next;
printf("删除成功\n");
return head;
}else if(p1==NULL){
printf("删除失败\n");
return head;
}
}
}
struct Student * insert(struct Student * head){
int i,n;
struct Student *p1,*p2;
p2=head;
p1=(struct Student *)malloc(sizeof(struct Student));
printf("请输入要插入的 位置,学号\n");
scanf("%d,%d",&n,&p1->num);
i=1;
while(p2!=NULL&&i<n-1){
p2=p2->next;
i++;
}
if(p2==NULL){
printf("插入失败\n");
return head;
}else
if(n==1){
p1->next=p2;
p2=p1;
printf("插入成功\n");
return p2;
}else{
p1->next=p2->next;
p2->next=p1;
printf("插入成功!\n");
return head;
}
}