编辑代码

#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){//每次循环都令p1=p1->next,
    //所以最后判断p是否为空来保证将最后一个数字也输出
        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;//a为要删除的学生学号
    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;
    }
}

// int delect(struct Student * head){//删除头结点无法实现
//     struct Student *p1,*p2;
//     p1=head;
//     p2=p1;
//     int w=0,i=1;//a为要删除的学生学号
//     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){
//             p1=p1->next;
//             printf("%d@@@@@\n",p1->num);
//             return 1;
//         }
//         if(i==w){
//             p2->next=p1->next;
//             return 1;
//         }else if(p1==NULL){
//             return 0;
//         }
//     }
// }