用链表构建学生信息库

一、 实验目的

建立一个学生成绩信息(包括学号、姓名、成绩)的单向链表,学生记录按照学号由小到大顺序排列,要求实现对成绩信息的创建、插入、删除和遍历操作。

二、 代码

#include

#include

#include

struct stud_node

{

    int num;

    char name[20];

    int score;

    struct stud_node*next;

};

struct stud_node*Create_Stu_Doc();

struct stud_node*InsertDoc(struct stud_node*head,struct stud_node*stud);

struct stud_node*DeleteDoc(struct stud_node*head,int num);

void Print_Stu_Doc(struct stud_node*head);

 

int main(void)

{

    struct stud_node*head,*p;

    int choice,num,score;

    char name[20];

    int size=sizeof(struct stud_node);

    do{

        printf("1:Create 2:Insert 3:Delete 4:Print 0:Exit\n");

        scanf("%d",&choice);

        switch(choice){

        case 1:

            head=Create_Stu_Doc();

            break;

        case 2:

            printf("Input num,name and score\n");

            scanf("%d%s%d",&num,name,&score);

            p=(struct stud_node*)malloc(size);

            p->num=num;

            strcpy(p->name,name);

            p->score=score;

            head=InsertDoc(head,p);

            break;

        case 3:

            printf("Input num:\n");

            scanf("%d",&num);

            head=DeleteDoc(head,num);

            break;

        case 4:

            Print_Stu_Doc(head);

            break;

        case 0:

            break;

        }

    }while(choice!=0);

    return 0;

}

struct stud_node*Create_Stu_Doc()

{

    struct stud_node*head,*p;

    int num,score;

    char name[20];

    int size=sizeof(struct stud_node);

    head=NULL;

    printf("Input num,name and score\n");

    scanf("%d%s%d",&num,name,&score);

    while(num!=0)

    {

        p=(struct stud_node*)malloc(size);

        p->num=num;

        strcpy(p->name,name);

        p->score=score;

        head=InsertDoc(head,p);

        scanf("%d%s%d",&num,name,&score);

    }

    return head;

};

struct stud_node*InsertDoc(struct stud_node*head,struct stud_node*stud)

{

    struct stud_node*ptr,*ptr1,*ptr2;

    ptr2=head;

    ptr=stud;

    if(head==NULL)

    {

        head=ptr;

        head->next=NULL;

    }

    else

    {

        while((ptr->num>ptr2->num)&&ptr2->next!=NULL)

        {

            ptr1=ptr2;

            ptr2=ptr2->next;

        }

        if(ptr->num<=ptr2->num)

        {

            if(head==ptr2)

                head=ptr;

            else ptr1->next=ptr;

            ptr->next=ptr2;

        }

        else

        {

            ptr2->next=ptr;

            ptr->next=NULL;

        }

    }

    return head;

}

struct stud_node*DeleteDoc(struct stud_node*head,int num)

{

    struct stud_node*ptr1,*ptr2;

    while(head!=NULL&&head->num==num)

    {

        ptr2=head;

        head=head->next;

        free(ptr2);

    }

    if(head==NULL)

    {

        return NULL;

    }

    ptr1=head;

    ptr2=head->next;

    while(ptr2!=NULL)

    {

        if(ptr2->num==num)

        {

            ptr1->next=ptr2->next;

            free(ptr2);

        }

        else

            ptr1=ptr2;

        ptr2=ptr1->next;

    }

    return head;

}

void Print_Stu_Doc(struct stud_node*head)

{

    struct stud_node*ptr;

    if(head==NULL)

    {

        printf("\n No Records\n");

        return;

    }

    printf("\nThe Students'Records Are :\n");

    printf("Num\tName\tScore\n");

    for(ptr=head;ptr!=NULL;ptr=ptr->next)

        printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);

}

三、 运行截图

 

四、 增加一个修改函数,实现对链表中某结点信息的修改。

代码:

#include

#include

#include

struct stud_node

{

    int num;

    char name[20];

    int score;

    struct stud_node*next;

};

struct stud_node*Create_Stu_Doc();

struct stud_node*InsertDoc(struct stud_node*head,struct stud_node*stud);

struct stud_node*DeleteDoc(struct stud_node*head,int num);

struct stud_node*UpdateDoc(struct stud_node*head,int num,int score);

void Print_Stu_Doc(struct stud_node*head);

int main(void)

{

    struct stud_node*head,*p;

    int choice,num,score;

    char name[20];

    int size=sizeof(struct stud_node);

    do{

        printf("1:Create 2:Insert 3:Delete 4:Print 5:Update 0:Exit\n");

        scanf("%d",&choice);

        switch(choice){

        case 1:

            head=Create_Stu_Doc();

            break;

        case 2:

            printf("Input num,name and score\n");

            scanf("%d%s%d",&num,name,&score);

            p=(struct stud_node*)malloc(size);

            p->num=num;

            strcpy(p->name,name);

            p->score=score;

            head=InsertDoc(head,p);

            break;

        case 3:

            printf("Input num:\n");

            scanf("%d",&num);

            head=DeleteDoc(head,num);

            break;

        case 4:

            Print_Stu_Doc(head);

            break;

        case 5:

            printf("请输入要修改的学生学号以及要修改的分数:\n");

            scanf("%d%d",&num,&score);

            head=UpdateDoc(head,num,score);

            break;

        case 0:

            break;

        }

    }while(choice!=0);

    return 0;

}

struct stud_node*Create_Stu_Doc()

{

    struct stud_node*head,*p;

    int num,score;

    char name[20];

    int size=sizeof(struct stud_node);

    head=NULL;

    printf("Input num,name and score\n");

    scanf("%d%s%d",&num,name,&score);

    while(num!=0)

    {

        p=(struct stud_node*)malloc(size);

        p->num=num;

        strcpy(p->name,name);

        p->score=score;

        head=InsertDoc(head,p);

        scanf("%d%s%d",&num,name,&score);

    }

    return head;

};

struct stud_node*InsertDoc(struct stud_node*head,struct stud_node*stud)

{

    struct stud_node*ptr,*ptr1,*ptr2;

    ptr2=head;

    ptr=stud;

    if(head==NULL)

    {

        head=ptr;

        head->next=NULL;

    }

    else

    {

        while((ptr->num>ptr2->num)&&ptr2->next!=NULL)

        {

            ptr1=ptr2;

            ptr2=ptr2->next;

        }

        if(ptr->num<=ptr2->num)

        {

            if(head==ptr2)

                head=ptr;

            else ptr1->next=ptr;

            ptr->next=ptr2;

        }

        else

        {

            ptr2->next=ptr;

            ptr->next=NULL;

        }

    }

    return head;

}

struct stud_node*DeleteDoc(struct stud_node*head,int num)

{

    struct stud_node*ptr1,*ptr2;

    while(head!=NULL&&head->num==num)

    {

        ptr2=head;

        head=head->next;

        free(ptr2);

    }

    if(head==NULL)

    {

        return NULL;

    }

    ptr1=head;

    ptr2=head->next;

    while(ptr2!=NULL)

    {

        if(ptr2->num==num)

        {

            ptr1->next=ptr2->next;

            free(ptr2);

        }

        else

            ptr1=ptr2;

        ptr2=ptr1->next;

    }

    return head;

}

void Print_Stu_Doc(struct stud_node*head)

{

    struct stud_node*ptr;

    if(head==NULL)

    {

        printf("\n No Records\n");

        return;

    }

    printf("\nThe Students'Records Are :\n");

    printf("Num\tName\tScore\n");

    for(ptr=head;ptr!=NULL;ptr=ptr->next)

        printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);

}

struct stud_node*UpdateDoc(struct stud_node*head,int num,int score)

{

    struct stud_node *p;

    for(p=head;p;p=p->next)

{

        if(p->num==num)

        {

            p->score=score;

        }

}

return head;

}

运行截图

 

五、 实验结果

链表还需要多多练习。

你可能感兴趣的:(实验问题)