• 复习下C 链表操作(单向链表)


    Object-C 作为C 的包装语言(运行时、消息机制)。如果不熟悉C 的话实在玩得太肤浅。 随便深入oc 内部都会接触到C。

    runtime 、GCD、Block、消息机制。。。

    所有强大的功能无不用的 包装体(struct 结构体)。使用GCC/Clang (可以google 下。Clang 比GCC 更优化,) 编译指令。 转换OC 为 C 。

    终端 使用Clang 命令参考 

    clang -rewrite-objc file.m

    查看file.cpp 文件为编译转换的C

     

    单向链表 创建 、插入 、反转

     //struct
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct Student_st
    {
        char name[10];
        int  point;
       struct Student_st *stu;
    } Student;
    
    void CheckIsLinkTable(Student *student){
        Student *next = student->stu;
        if (next == NULL) {
            printf("查询失败,不是链式结构
    ");
            exit(0);
        }
    }
    
    Student * CreateLink_Table(int num){
        printf("num is %d
    ",num);
        
        Student * head,*current,*temp;
        //分配内存
        head = (Student *)malloc(sizeof(Student));
        if (head == NULL) {
            printf("内存分配失败 
    ");
            exit(0);
        }
        
        head ->name[0] = '';
        head ->point = 0;
        head -> stu = NULL;
        
        temp = head;
        for (int i = 0; i< num; i++) {
            current = (Student *)malloc(sizeof(Student));
            if (current == NULL) {
                printf("内存分配失败 
    ");
                exit(0);
            }
            current ->stu = NULL;
    
            
            printf("请输入第%d位学生姓名和成绩 如:name|89 
    ",i+1);
            char stuName[10] = "未知";
            int student_point = 0;
            char argValue[15] = "未知";
            
            scanf("%s",argValue);
            
            if (strstr(argValue,"|")==NULL) {
                printf("请使用|分割内容 Y|N Y 重新输入,N 退出
    ");
                char content[5];
                scanf("%s",content);
                if (strcmp(content,"Y")==0) {
                    scanf("%s",argValue);
                }else{
                    exit(0);
                }
            }
            
            //分割字符串
            char *token;
    
            token = strtok(argValue,"|");
            int i = 0;
            while (token != NULL) {
                if (i>0) {
                    student_point = atoi(token);
                    printf("%d
    ",student_point);
                }else{
                    ////char*的值赋给数组怎么做?----> c是常量指针,不可以改变,用
                    strncpy(stuName,token,sizeof(stuName));
                    printf("%s
    ",token);
                }
                
                token = strtok(NULL,"|");
                i++;
            }
            strncpy(current->name,stuName,sizeof(current->name));
            current ->point = student_point;
            
            current->stu = NULL;
            temp ->stu = current;
            temp = current;
            
        }
        
        return head;
    }
    
    //查询
    void selectStudent(Student *student){
        CheckIsLinkTable(student);
    
        Student *next = student->stu;
        int i =0;
        while (next) {
            printf("index %d; studentName is %s;  point is %d
    ",i+1,next->name,next->point);
            next = next ->stu;
            i++;
        }
    }
    //插入
    Student * insertStudentLinkTable(Student *student,char *insertContent){
        //分割字符串
        if (!insertContent) {
            exit(0);
        }
        
        //分割字符串
        char *token;
        char stuName[10];
        char preStuName[10];
        int student_point;
        token = strtok(insertContent,"|");
        int i = 0;
        while (token != NULL) {
            if (i==1) {
                ////char*的值赋给数组怎么做?----> c是常量指针,不可以改变,用
                strncpy(stuName,token,sizeof(stuName));
                printf("%s
    ",token);
            }else if(i==2){
                student_point = atoi(token);
                printf("%d
    ",student_point);
            }else{
                strncpy(preStuName,token,sizeof(preStuName));
                printf("%s
    ",token);
            }
            
            token = strtok(NULL,"|");
            i++;
        }
        Student *preStu;
        
        CheckIsLinkTable(student);
        //查找节点名字是否存在
        Student *next = student->stu;
        int ind = 0;
        while (next) {
            if ((next->name) && strcmp(preStuName,next->name)==0) {
                printf("找到节点%s 位于第%d个节点 开始插入节点
    ",preStuName,ind+1);
                preStu = next;
                break;
            }
            next = next->stu;
            ind++;
        }
        
        Student *nextStu = preStu->stu;
        //创建节点
        Student *newStu = (Student *)malloc(sizeof(Student));
        if (newStu==NULL) {
            printf("分配内存失败");
            exit(0);
        }
        strncpy(newStu->name,stuName,sizeof(char[10]));
        newStu->point = student_point;
        newStu->stu = nextStu;
        
        preStu->stu = newStu;
        
        //查询节点
        selectStudent(student);
        return student;
    }
    
    //反转
    Student *ReversionStudentLinkTable(Student *student){
        CheckIsLinkTable(student);
        
        Student *next,*current,*newLinkTable;
        next = NULL;
        current =NULL;
        newLinkTable = NULL;
        current = student->stu;
        while (current) {
            //保存游标节点 以后的节点
            next = current->stu;
            //断开游标节点后面的节点,并设置游标节点的后续节点为 新链表的第一个节点
            current->stu = newLinkTable;
            //保存当前游标节点 为新链表
            newLinkTable = current;
            //重置游标节点
            current = next;
        }
        
        //给游标节点添加 头节点
        Student *head = NULL;
        head = (Student *)malloc(sizeof(Student));
        head->name[0]='';
        head->point = 0;
        head->stu = newLinkTable;
        
        return head;
    }
    int main(void){
        char sf[15];
        
        /**
         *  创建单向链表
         */
        int num;
        printf ("请输入学生人数
    ");
        scanf("%d",&num);
        Student *link_stu = CreateLink_Table(num);
    
        
        /**
         *  单向链表插入节点
         */
        printf ("请插入节点内容 在 已存在节点名字的后面,如 已存在节点名字|待插入名字|待插入分数 
    ");
        scanf("%s",sf);
    
        link_stu = insertStudentLinkTable(link_stu,sf);
        
        /**
         *  反转单向链表
         */
        printf("反转链表Y|N 
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
           Student *newLt= ReversionStudentLinkTable(link_stu);
            //查询
            selectStudent(newLt);
        }
    return 0;
    }
    
    
    
     

     

     

  • 相关阅读:
    SP1716 GSS3
    A Simple Problem with Integers题解
    P4528 [CTSC2008]图腾 题解
    P1498 南蛮图腾 题解
    P2024 [NOI2001]食物链 题解
    Windows编程 Windows程序的生与死(中)
    Windows编程 Windows程序的生与死(上)
    C#实现在注册表中保存信息
    沿路径动画(Animation Along a Path)
    倾斜动画(SkewTransform)
  • 原文地址:https://www.cnblogs.com/DamonTang/p/4121204.html
Copyright © 2020-2023  润新知