• C语言实现单链表-04版


    前面的版本似乎没能让项目经理满意,他还希望这个链表有更多的功能;

    我们接下来要解决几个比较简单的功能;

    Problem

    1,更加友好的显示数据;

    2,能够通过名字删除节点;

     Solution

    首先我们编写一个简单的提示信息的函数;

    void when(char * tips){
        time_t rawtime;
        struct tm* timeinfo;
        time(&rawtime);
        timeinfo = localtime(&rawtime);
        char * timestr = asctime(timeinfo);
        int len = strlen(timestr);
        timestr[len-1]='	';
        printf("%s[%s]: ",timestr,tips);
        return ;
    }

    为啦使when成功的调用时间函数,你需要引入如下头文件;

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>

    接下来,我们接编写这个版本的主角函数;

    void delete_by_name(Link_List * lst, char *name)
    {
        Node * target = lst->head;
        Node * tmp = target;
    
        if(!strcmp(lst->head->data->name,name)){
            lst->head = lst->head->next;
            //free(target);
            when("INFO");
            puts("Delete successful");
            lst->size--;
            return ;
        }
    
        while (target != NULL ){
            if(!strcmp(target->data->name,name)) {
                tmp->next = target->next;
                free(target);
                when("INFO");
                puts("Delete successful");
                lst->size--;
                return ;
            }
            tmp = target;
            target = target->next;
        }
        when("ERROR");
        puts("Cannot delete it ");
        return ;
    }

    通过查询相应的名字,然后删除该节点;

    实现的方式可能有更加简单的,这里我们只是自己的一种即兴写法;

    没有论证过什么时间,空间复杂度;

    细心的话可能会发现我的内存有严重泄漏问题;

    如果继续版本更新的话我会解决的;

    好啦!,说说这个函数其实很简单,首先判断节点是否为链表头部节点;

    如果是,那么让链表直接指向下一个节点,忽略它;(issue code)

    我说issue code通常表明这里存在问题,日后再解决;

    第二部分,进行遍历,发现就进行跳过处理;

    接下来我们直接给出所有代码啦:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    void when(char * tips){
        time_t rawtime;
        struct tm* timeinfo;
        time(&rawtime);
        timeinfo = localtime(&rawtime);
        char * timestr = asctime(timeinfo);
        int len = strlen(timestr);
        timestr[len-1]='	';
        printf("%s[%s]: ",timestr,tips);
        return ;
    }
    
    typedef struct person_ {
        int hight;
        int weight;
        char name[32];
    }Person;
    
    typedef struct node_ {
        Person *data;
        struct node_ * next;
    }Node;
    
    typedef struct link_list_ {
        Node * head;
        Node * tail;
        int size;
    }Link_List;
    
    Node * create_node(void *data)
    {
        Node * tmp = (Node*)malloc(sizeof(Node));
        if(tmp) {
            tmp->data = data;
            tmp->next = NULL;
        }
        return tmp;
    }
    
    Link_List * init_list(void)
    {
        Link_List * lst = (Link_List*)malloc(sizeof(Link_List));
        lst->head = lst->tail = NULL;
        lst->size = 0;
        return lst;
    }
    void insert(Link_List * lst,void *data)
    {
        Node * new_node = create_node(data);
        if(lst->size == 0) {
            lst->tail = new_node;
        }
        new_node->next = lst->head;
        lst->head = new_node;
        lst->size++;
    }
    
    void travel(Link_List * root)
    {
        puts("----------------------------------------------------------");
        printf("---------------------------------------------------------->>itmes[%d]
    ",root->size);
        if(root) {
            Node * we = root->head;
            while(we != NULL) {
                printf("Name:%s		Hight:%d		Weight:%d
    ",we->data->name,we->data->hight,we->data->weight);
                we = we->next;
            }
        }
        puts("----------------------------------------------------------");
        return ;
    }
    
    
    Node * search_by_name(Link_List * lst, char * name)
    {
        Node * target = lst->head;
        while (target != NULL ){
            if(!strcmp(target->data->name,name)) {
                when("INFO");
                puts("Fount it");
                return target;
            }
            target = target->next;
        }
        when("WARNING");
        printf("Cannot Found (%s)
    ",name);
        return NULL;
    }
    
    void node_detail(Node * node)
    {
        if(node) {
            puts("----------------------------------------------------------");
            printf("Name:%s		Hight:%d		Weight:%d
    ",node->data->name,node->data->hight,node->data->weight);
            puts("----------------------------------------------------------");
        }
        return ;
    }
    void append_after(Link_List * lst, Node * new,Node * old)
    {
        Node * target = lst->head;
        while(target != NULL){
            if(target == old){
                new->next = old->next;
                old->next = new;
                when("INFO");
                puts("Append successful");
                lst->size++;
                return ;
            }
            target = target->next;
        }
        return ;
    }
    
    void insert_before(Link_List *lst, Node * new, Node * old)
    {
        Node * target = lst->head;
        while(target->next != NULL){
            if(target->next == old){
                new->next = target->next;
                target->next = new;
                when("INFO");
                puts("Insert successful");
                lst->size++;
                return ;
            }
            target->next = target->next->next;
        }
        return ;
    }
    
    void delete_by_name(Link_List * lst, char *name)
    {
        Node * target = lst->head;
        Node * tmp = target;
    
        if(!strcmp(lst->head->data->name,name)){
            lst->head = lst->head->next;
            //free(target);
            when("INFO");
            puts("Delete successful");
            lst->size--;
            return ;
        }
     while (target != NULL ){
            if(!strcmp(target->data->name,name)) {
                tmp->next = target->next;
                free(target);
                when("INFO");
                puts("Delete successful");
                lst->size--;
                return ;
            }
            tmp = target;
            target = target->next;
        }
        when("ERROR");
        puts("Cannot delete it ");
        return ;
    }
    
    
    
    int main(void)
    {
        Link_List * root = init_list();
    
        Person a = { 60, 170, "Frank" };
        Person b = { 70, 180, "Jack" };
        Person c = { 80, 190, "Landpack" };
    
        insert(root,&a);
        insert(root,&b);
        insert(root,&c);
    
        //test travel function 
        travel(root);
       // test search node
        Node * result = search_by_name(root,"Jack");
        node_detail(result);
    
        search_by_name(root,"NoName");
        // test append function 
        Person d = { 88,180,"Peter" };
        Node * new_d = create_node(&d);
        append_after(root,new_d,result);
        travel(root);
    
        // test insert function 
        Person e = { 77,170,"Sap" };
        Node * new_e = create_node(&e);
        insert_before(root,new_e,result);
        travel(root);
    
        delete_by_name(root,"Landpack");
        travel(root);
        delete_by_name(root,"Frank");
        travel(root);
        delete_by_name(root,"Sap");
        travel(root);
    
    
        return 0;
    }

    代码就这样啦哈!看看运行结果;

    Discussion

    故事总该结束啦!我知道项目经理的脾气;

    他又不给涨工资,整天让我们加班;

    我可受够啦!这个链表项目其实还有很多有趣的内容;

    我其实是喜欢这份工作的,但是有时候心情不好;

    就像是今天,本来想好好的写第4版的,结果只能大致略过啦很多细节;

    不给内容不多,挺好理解,也希望大家原谅哈;

    如果我还有机会的话会继续更新版本的;

    我们还有,双向链表,循环链表,队列,栈要实现呢;

    到时候,我们会充分利用起链表的各种特性;

    See you !

    Can we drop this masquerade
  • 相关阅读:
    CSS3新增文本属性
    CSS选择器
    【转】Java基础——面试题汇总
    【转】equals和==的区别
    【转】JRE和JDK的区别
    【转】深度学习常用的模型评估指标
    【转】使用Scanner输入字符串时next()和nextLine()区别
    JAVA操作ORACLE大对象
    ORACLE大对象存储
    iis是什么东西?
  • 原文地址:https://www.cnblogs.com/landpack/p/4823787.html
Copyright © 2020-2023  润新知