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] = '\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;
}
随机推荐
- Can't read [proguard.ClassPathEntry@1a0c10f] (No such file or directory)
//该错误无法识别,是因为你使用了旧版本的proguard,建议你下载新版本4.5以上的proguard来替代Android SDK中使用的旧版sdcard 这样你就可以知道错误所在.因为只有4.5版 ...
- discuz密码找回:[1]忘记UCENTER创始人密码
人们都是健忘的,何况每天的事情很多,有些站长更是兼职做,赚点外快而已,而ucenter更是不常用,所以忘记密码是在正常不过的事情,如果密码忘记怎么找回呢?方法有很多种,例如用comsenz tools ...
- 深入探讨:MySQL数据库MyISAM与InnoDB存储引擎的比较
From: http://www.jb51.net/article/37766.htm MySQL有多种存储引擎,MyISAM和InnoDB是其中常用的两种.这里介绍关于这两种引擎的一些基本概念(非深 ...
- Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)
Dubbo与Zookeeper.Spring整合使用 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spri ...
- UNIX环境编程学习笔记(17)——进程管理之进程的几个基本概念
lienhua342014-10-05 1 main 函数是如何被调用的? 在编译 C 程序时,C 编译器调用链接器在生成的目标可执行程序文件中,设置一个特殊的启动例程为程序的起始地址.当内核执行 C ...
- Win7下静态编译QT5.12源码
官方参考文档:https://doc.qt.io/qt-5/build-sources.html CSDN博客:https://blog.csdn.net/GG_SiMiDa/article/deta ...
- CIO需加强对战略管理层面的掌控-精华篇
当代CIO面临提升信息化作用的新机遇.CIO在企业中,不能满足于职能性的技术支撑角色,要找到新的着力点,以发挥信息化在全局战略中的作用,把信息化力量聚焦于做强做优,提高国际竞争力上来,成为企业不可或缺 ...
- 怎样把网站升级到http/2
https://juejin.im/post/59c63adf6fb9a00a4c271484
- Mybatis -- 批量更新 -- updateBatch
mysql数据库配置: 数据库连接必须配置:&allowMultiQueries=true并且‘&’ 用&替换 jdbc.url=jdbc:mysql://192.168.10 ...
- Linux od命令(以指定进制显示文件)
从“读取二进制文件”出发,到od命令的使用 在桃村实习期间,一直努力做毕业设计,我的毕业设计中有一个内容就是读取SEGY文件.在读取文件时,经常遇到的问题时你要读取浮点型数据,这时你就必须考虑你所使用 ...