// 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. java 项目 存入mysql后 变问号 MySql 5.6 (X64) 解压版 1067错误与编码问题的解决方案

    [参考]MySQL 5.7.19 忘记密码 重置密码 my.ini示例 服务启动后停止 环境 Java环境JDK1.8  安装好了 mysql-5.6.38-winx64  idea2016(64) ...

  2. linux命令中的head命令

    head命令和tail命令就像他的名字一样浅显易懂,它是用来显示开头或者结尾某个数量的文字区块,head用来显示档案的开头至标准输出当中,而tail想当然就是查看档案的结尾. 命令格式 head  [ ...

  3. Docker命令之 save

    docker save : 将指定镜像保存成 tar 归档文件. 语法 docker save [OPTIONS] IMAGE [IMAGE...] OPTIONS说明: -o :输出到的文件. 实例 ...

  4. iOS:ODRefreshControl

    https://github.com/Sephiroth87/ODRefreshControl Important note if your project doesn’t use ARC: you ...

  5. 同一个Tomcat部署两个project之间的通信问题

    同一个tomcat下的两个project是无法通信的. 同一个tomcat中的project能互相调用吗 启动一个tomcat部署多个项目,那么每个项目算是一个线程还是进程呢? Tomcat中的pro ...

  6. redis 的set数据类型

    相关命令 1.SADD SADD key-name item1 [item 2…] 将一个或多个成员元素加入到集合中 2.SREM SMEMBERS  key-name item1 [item 2…] ...

  7. html 内联函数宽度设置

    width and/or height in tables are not standard anymore; as Ianzz says, they are depreciated. Instead ...

  8. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

    if (!string.IsNullOrEmpty(FarmWorkId)) { data = data.Where(p => p.TypeId == Convert.ToInt32(FarmW ...

  9. 泛泰A870K去掉相机快门声音的方法

    首先ROOT手机,挂载读写,/system/media/audio/ui里面哈,把camera-click.ogg改成camera-click.ogg.bak就可以了 转载自:http://bbs.9 ...

  10. Java查看类的成员

    在一个类的内部,一般包括以下几类成员:成员变量.构造方法.普通方法和内部类等.使用反射机制可以在无法查看源代码的情况下查看类的成员.编写程序,使用反射机制查看ArrayList类中定义的成员变量.构造 ...