zoukankan      html  css  js  c++  java
  • c实现的list

    // clist.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <stdio.h>
    #include <malloc.h> //动态分配内存
    #include <stdlib.h> //exit 函数
    #include <stdbool.h> //布尔值函数
    #include <string.h>

    struct node {
        char * str;
        int len;
        node* next;
        //node* first;
        //node* last;
    };

    node* first = NULL;
    node* last = NULL;
    node* prev = NULL;
    node* search(const char * str);


    /*node* del(node* list) {
        if (list == NULL)
            return;
        free(list->str);
        return list->next;
    }*/

    node* new_node() {
        node* n = (node*)malloc(sizeof(node) + 1);
        return n;
    }

    node* first_node() {
        return first;
    }

    node* last_node() {
        return last;
    }

    void delete_node(const char* str) {
        node* node_to_del = search(str);
        prev->next = node_to_del->next;
        free(node_to_del->str);
        node_to_del->str = NULL;
        free(node_to_del);
        node_to_del = NULL;
        prev = first;
    }

    void update(const char* str, const char* newstr) {
        node* node_to_update = search(str);
        strcpy(node_to_update->str, newstr);
        node_to_update->len = strlen(newstr);
        node_to_update->str[node_to_update->len] = '';
    }

    node* list_init(const char* first_string) {
        first = last = new_node();
        first->str = (char*)malloc(sizeof(first_string)+1);
        strcpy(first->str, first_string);
        first->len = strlen(first_string);
        first->str[first->len] = '';
        first->next = NULL;
        return first;
    }

    void add(const char * str) {
        node* n = new_node();
        n->str = (char*)malloc(sizeof(str)+1);
        n->next = NULL;
        n->len = strlen(str);
        n->str[n->len] = '';
        strcpy(n->str, str);
        last->next = n;
        last = n;
    }

    node* search(const char * str) {
        node* n = first;
        prev = first;
        while (n != NULL && n->next != NULL) {
            if (strcmp(str, n->str) == 0)
                return n;
            prev = n;
            n = n->next;
        }
        return n;
    }

    int main()
    {
        node* head = list_init("head");
        add("hello,first");
        add("hello,second");
        add("cc");
        add("dd");
        add("ee");
        add("ea");
        add("eb");
        add("ec");
        add("ed");
        add("ee");

        node* dd_node = search("ee");
        delete_node("ee");
        update("hello,second", "second");
        return 0;
    }


  • 相关阅读:
    嵌入式系统WinCE下应用程序GUI界面开发【转】
    pidstat 命令详解
    shell脚本检查是否存在tun0虚拟网卡,若不不存在服务器更改port,并重启服务器,客户端修改port,并重新启动客户端
    阿里云k8s部署zookeeper集群
    Kubernetes StatefulSet
    dubbo-admin管理平台搭建
    k8s pv,pvc无法删除问题
    Linux配置AndroidSDK&Jenkins远程部署
    CentOS的vsftp修改默认配置路径方法
    Java语法糖3:泛型
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/8611926.html
Copyright © 2011-2022 走看看