• 【c语言编程基础】结构体单向链表的基本操作


    前言

    关注点

    code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> // strcat
    #define Size 4
    typedef struct Table{ 
        int len;
        int size;
        int aa;
    }Table;
    
    typedef struct TableNode{
        Table ta;
        struct TableNode *next;
    } TableNode;
    
    bool table_eq(Table a, Table b){
        bool eq = false;
        if((a.len == b.len) && (a.size==b.size)) eq=true;
        return eq;
    }
    TableNode *trackers = NULL;
    int nodelen = 0;
    // Table *t1=(Table*)calloc(Size, sizeof(Table));
    void trackernode_append(Table tracker){
        printf("trackernode_append\n");
        TableNode* p = trackers;
        TableNode* tmp = (TableNode*)malloc(sizeof(TableNode));
        tmp->ta = tracker;
        tmp->next = NULL;
        if(trackers==NULL) trackers=tmp;
        else{    
            TableNode* head = trackers;
            while(head->next){ head = head->next;}
            head->next = tmp;
        }
        nodelen++;
        trackers = p;   
    }
    void trackernode_delete_val(Table tracker){
        printf("trackernode_delete_val\n");
        TableNode* head = trackers;
        if(table_eq(head->ta, tracker)) {
            TableNode* tmp = head;
            if(head->next) { head = head->next; trackers=head; }
            else trackers=NULL;
            free(tmp); 
        }else{
            TableNode* pre = head;
            TableNode* cur = head->next;
            while(!table_eq(cur->ta,tracker) && (cur!=NULL)){
                pre = cur;
                cur = cur->next;
           }
           if(cur!=NULL) { pre->next = cur->next; free(cur); }
        }
        trackers = head;
    }
    void trackernode_delete_node(TableNode* delnode){
        // TODO 
        // TableNode* head = trackers;
    
    }
    
    void trackernode_update(Table old, Table newd){
        printf("trackernode_update\n");
        TableNode* head = trackers;
        while(head){
            if(table_eq(head->ta,old)) { head->ta = newd; return; }
            head = head->next;
        }
    }
    
    int main()
    {
        char a[8] = {0};
        strcat(a, "circle_yellow: 0.99999");
        printf("a: %s\n", a);
        int arr[Size] = {2, -1, 0, 1};
        int *p = arr;
        TableNode *head=(TableNode*)malloc(sizeof(TableNode));
        Table ta;
        ta.len = 0;
        ta.size = 0; 
        head->ta = ta;
        head->next = NULL;
        trackers = head;
        nodelen++;
        for(int i=1; i<Size; i++){
            ta.len = i;
            ta.size = i;
            TableNode *tmp=(TableNode*)malloc(sizeof(TableNode));
            tmp->ta = ta;
            tmp->next = NULL;
            head->next = tmp;
            head = tmp;
            nodelen++;
        }
        //
        Table tracker_arr[Size];
        head = trackers;
        int k=0;
        while(head){
            printf("k: %d\n", k);
            tracker_arr[k] = head->ta;
            head=head->next;
            k++;   
        }
        printf("k: %d, Size: %d\n", k, Size);
        for(int i=0; i<k; i++){
            if(p[i]==-1){
                // delete
                trackernode_delete_val(tracker_arr[i]);
                nodelen--;       
            }else{
                // update
                Table newdata;
                newdata.len = 6;
                newdata.size = 8;
                trackernode_update(tracker_arr[i], newdata);
            }
        }
        Table newdata;
        newdata.len = 3;
        newdata.size = 3;
        trackernode_append(newdata);
        head=trackers;
        while(head){
            printf("len: %d, size: %d, aa: %d\n", (head->ta).len, (head->ta).size, (head->ta).aa);
            head = head->next;
        }
        head= trackers;
        int i=0;
        while(head){
            i++;
            printf("i: %d\n", i);
            if(head->next) { printf("while\n"); head = head->next; }
            else {printf("break\n"); break; }
        }
        printf("************i: %d\n", i);
        printf("nodelen: %d\n", nodelen);
        
        return 0;
    }
    View Code

  • 相关阅读:
    HttpClient
    spring入门
    morphia进阶
    morphia基本API方法
    mangodb数据库框架morphia注解
    学与思
    解决vscode执行yarn启动项目报错
    使用swiper+动画实现轮播图自动播放
    vue中使用el-tree实现一行显示多条数据
    使用git命令提交部分修改代码
  • 原文地址:https://www.cnblogs.com/happyamyhope/p/16744203.html
Copyright © 2020-2023  润新知