// 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] = '\0';
}

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] = '\0';
    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] = '\0';
    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;
}

随机推荐

  1. Linux账号和权限管理

    一. 用户和组的管理  - Linux中用户种类 种类 特点 root 是管理员,拥有至高无上的权限,不受限制,UID为0 普通用户 管理员创建的用户,受权限限制,UID一般从500开始,可以登录系统 ...

  2. Linq中string转int的方法

    Linq中string转int的方法   在做批量删除时,需把一串id值所对应的数据删除,调试出现问题: Linq语句中如果使用ToString()进行类型转换,编译时不会报错,但执行时会出现如下错误 ...

  3. Idea配置CheckStyle

    1.安装CheckStyle 2.配置idea_checks.xml 3.启用CheckStyle idea_checks.xml: <?xml version="1.0"? ...

  4. 使用 ML Pipeline 构建机器学习工作流

    http://www.ibm.com/developerworks/cn/opensource/os-cn-spark-practice5/

  5. 为tomcat指定JDK版本

    可以为tomcat指定JDK. 当系统中安装有多个版本的jdk时,可以为tomcat指定jdk版本. 在catalina.sh文件和setclasspath.sh文件开头的空白处加上如下一行: exp ...

  6. vue加百度统计代码(亲测有效)

    申请百度统计后,会得到一段JS代码,需要插入到每个网页中去,在Vue.js项目首先想到的可能就是,把统计代码插入到index.html入口文件中,这样就全局插入,每个页面就都有了;这样做就涉及到一个问 ...

  7. C#文件操作工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. geoserver入门

    1.什么是Universal Transverse Mercator system 翻译名叫做通用横轴魔卡托系统,通常称为UTM.这个投影系从中心子午线把世界分成一系列6度的纵向宽区域. 2.什么是w ...

  9. Spring的JdbcTemplate与其事务

    JdbcTemplate:http://lehsyh.iteye.com/blog/1579737 JdbcTemplate with TransactionTemplate:http://www.c ...

  10. centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...