学生信息管理系统(数据结构 链表操作)

#include 
#include 
#include 

#if defined(WIN32)
#include
#define __CLS   "cls"
#elif defined(__unix)
#define __CLS   "clear"
#else
#define __CLS   ""
#endif

typedef struct student
{
    int num;
    int score;
    char name[10];
    struct student *next;
} STU;
void link_print(STU *head)
{
    STU *p = head;
    while (p != NULL)
    {
        printf("%d %d %s\n", p->num, p->score, &p->name);
        p = p->next;
    }
}
void link_creat_head(STU **p_head, STU *p_new)
{
    STU *p_mov = *p_head;
    if (*p_head == NULL) // 当第一次加入链表为空时,head执行p_new
    {
        *p_head = p_new;
        p_new->next = NULL;
    }
    else // 第二次及以后加入链表
    {
        while (p_mov->next != NULL)
        {
            p_mov = p_mov->next; // 找到原有链表的最后一个节点
        }
        p_mov->next = p_new; // 将新申请的节点加入链表
        p_new->next = NULL;
    }
}
STU *link_search_name(STU *head, char *name)
{
    STU *p = head;
    while (p != NULL)
    {
        if (strcmp(p->name, name) == 0)
        {
            return p;
        }
        p = p->next;
    }
    return NULL;
}
STU *link_search_num(STU *head, int num)
{
    STU *p = head;
    while (p != NULL)
    {
        if (p->num == num)
        {
            return p;
        }
        p = p->next;
    }
    return NULL;
}
void link_delete_name(STU **p_head, char *name)
{
    STU *q, *p;
    q = p = *p_head;
    if (*p_head == NULL)
    {
        printf("空链表,无法进行删除\n");
    }
    else
    {
        while (p->next != NULL && strcmp(p->name, name) != 0)
        {
            q = p;
            p = p->next;
        }
        if (strcmp(p->name, name) == 0)
        {
            if (p == *p_head)
            {
                *p_head = (*p_head)->next;
            }
            else
            {
                q->next = p->next;
            }
            free(p);
        }
        else
            printf("没有找到!\n");
    }
}
void link_delete_num(STU **p_head, int num)
{
    STU *q, *p;
    q = p = *p_head;
    if (*p_head == NULL)
    {
        printf("空链表,无法进行删除\n");
    }
    else
    {
        while (p->next != NULL && p->num != num)
        {
            q = p;
            p = p->next;
        }
        if (p->num == num)
        {
            if (p == *p_head)
            {
                *p_head = (*p_head)->next;
            }
            else
            {
                q->next = p->next;
            }
            free(p);
        }
        else
            printf("没有找到!\n");
    }
}
void link_insert_num(STU **p_head, STU *p_new)
{
    STU *p, *q;
    p = q = *p_head;
    if (*p_head == NULL)
    {
        *p_head = p_new;
        p_new->next = NULL;
    }
    else
    {
        while ((p_new->num > p->num) && p->next != NULL)
        {
            q = p;
            p = p->next;
        }
        if (p_new->num <= p->num)
        {
            if (p == *p_head)
            {
                p_new->next = *p_head;
                *p_head = p_new;
            }
            else
            {
                q->next = p_new;
                p_new->next = p;
            }
        }
        else
        {
            p->next = p_new;
            p_new->next = NULL;
        }
    }
}
void help()
{
    printf("********************************\n");
    printf("**0:帮助                      **\n");
    printf("**1:输入宿舍信息              **\n");
    printf("**2:遍历宿舍信息              **\n");
    printf("**3:按姓名查找                **\n");
    printf("**4:按学号查找                **\n");
    printf("**5:删除指定学号的人的信息    **\n");
    printf("**6:删除指定姓名的人的信息    **\n");
    printf("**7:插入新的学生信息          **\n");
    printf("**8:按姓名排序                **\n");
    printf("**9:按学号排序                **\n");
    printf("**10:实现链表的逆序           **\n");
    printf("**11:退出信息管理系统         **\n");
    printf("********************************\n");
    printf("请输入您的命令,0帮助:\n");
}
void link_free(STU **p_head)
{
    STU *p = *p_head;
    while (p != NULL)
    {
        p = (*p_head)->next;
        free(*p_head);
        *p_head = p;
    }
}
STU *order_num(STU *head)
{
    STU temp;
    STU *pf, *pb;
    pb = pf = head;
    if (head == NULL)
    {
        printf("list is null\n");
        return NULL;
    }
    while (pf->next != NULL)
    {
        pb = pf->next;
        while (pb != NULL)
        {
            if (pf->num > pb->num)
            {
                temp = *pb;
                *pb = *pf;
                *pf = temp;

                temp.next = pb->next;
                pb->next = pf->next;
                pf->next = temp.next;
            }
            pb = pb->next;
        }
        pf = pf->next;
    }
    return (head);
}
STU *order_name(STU *head)
{
    STU temp;
    STU *pf, *pb;
    pb = pf = head;
    if (head == NULL)
    {
        printf("list is null\n");
        return NULL;
    }
    while (pf->next != NULL)
    {
        pb = pf->next;
        while (pb != NULL)
        {
            if (strcmp(pf->name, pb->name) > 0)
            {
                temp = *pb;
                *pb = *pf;
                *pf = temp;

                temp.next = pb->next;
                pb->next = pf->next;
                pf->next = temp.next;
            }
            pb = pb->next;
        }
        pf = pf->next;
    }
    return (head);
}
STU *reverse(STU *head)
{
    STU *pf, *pb, *r;
    pf = head;
    pb = pf->next;

    while (pb != NULL)
    {
        r = pb->next;
        pb->next = pf;
        pf = pb;
        pb = r;
    }
    head->next = NULL;
    head = pf;
    return head;
}
int main()
{
    int i = 0;
    int num, num_num;
    char name[20];
    STU *p_new = NULL, *head = NULL, *p = NULL;
    help();
    while (1)
    {

        scanf("%d", &i);
        switch (i)
        {
        case 0:
            system(__CLS);
            help();
            break;
        case 1:
            printf("请输入链表的初始个数");
            scanf("%d", &num);
            for (i = 0; i < num; i++)
            {
                p_new = (STU *)malloc(sizeof(STU));
                printf("请输入学号、分数、名字\n");
                scanf("%d %d %s", &p_new->num, &p_new->score, p_new->name);
                link_creat_head(&head, p_new);
            }
            printf("请输入您的命令,0帮助:\n");
            break;
        case 2:

            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 3:
            printf("请输入您要查找的学生的姓名:\n");
            scanf("%s", name);
            p = link_search_name(head, name);
            if (p != NULL)
                printf("%d %d %s\n", p->num, p->score, &p->name);
            else
                printf("not found\n");
            printf("请输入您的命令,0帮助:\n");
            break;
        case 4:
            printf("请输入您要查找的学生的学号:\n");
            scanf("%d", &num_num);
            p = link_search_num(head, num_num);
            if (p != NULL)
                printf("%d %d %s\n", p->num, p->score, &p->name);
            else
                printf("not found\n");
            printf("请输入您的命令,0帮助:\n");
            break;
        case 5:
            printf("请输入您要删除的学生的学号:\n");
            scanf("%d", &num_num);
            link_delete_num(&head, num_num);
            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 6:
            printf("请输入您要删除的学生的姓名:\n");
            scanf("%s", name);
            link_delete_name(&head, name);
            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 7:
            printf("请输入要插入学生的信息:\n");
            p_new = (STU *)malloc(sizeof(STU));
            printf("请输入学号、分数、名字\n");
            scanf("%d %d %s", &p_new->num, &p_new->score, p_new->name);
            link_insert_num(&head, p_new);
            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 8:
            head = order_name(head);
            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 9:
            head = order_num(head);
            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 10:
            head = reverse(head);
            link_print(head);
            printf("请输入您的命令,0帮助:\n");
            break;
        case 11:
            return 0;
            break;
        default:
            break;
        }
    }
    return 0;
}

你可能感兴趣的:(链表,链表,数据结构)