• 【C语言程序设计第四版】例11-10代码


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct stud_node{
        int num;
        char name[20];
        int score;
        struct stud_node *next;
    };
    
    struct stud_node *Create_Stu_Doc(void);
    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=NULL, *p=NULL;
        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
    ");
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    head = Create_Stu_Doc();
                    break;
                case 2:
                    printf("Input num, name and score:
    ");
                    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: 
    ");
                    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:
    ");
        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;
        ptr1 = NULL;
        
        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;
        
        ptr1 = ptr2 = NULL;
        
        // 头部的就为符合条件的情况下,删除符合条件的节点
        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("
    No Records
    ");
            return;
        }
        printf("
    The Students'Records Are: 
    ");
        printf("Num	 Name	 Score
    ");
        for (ptr=head; ptr!=NULL; ptr=ptr->next) {
            printf("%d	%s	%d
    ", ptr->num, ptr->name, ptr->score);
        }
        
    }
  • 相关阅读:
    个人案例分析
    软工结对作业
    交点问题
    C语言复习
    【软件工程】提问回顾与个人总结
    【技术博客】Arxiv的新Paper获取和机翻
    【技术博客】动态面包屑导航
    对对碰 -- 软工结对编程博客
    交点计数 -- 软工个人项目作业
    面向对象的程序设计-模块四课程总结
  • 原文地址:https://www.cnblogs.com/sidianok/p/15335602.html
Copyright © 2020-2023  润新知