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;
}
随机推荐
- 用grep 筛选fastq 序列
grep 从文件中筛选出 包含指定的字符或者正则表达式的行:默认只打印匹配到的行, 比如一个文件 test.txt, 其内容为: abc def ghi jkl grep a test.txt, 输出 ...
- C# where用法解析
where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量.1.接口约束.例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就可以实现 ICompara ...
- 基于Redis实现分布式锁以及任务队列
一.前言 双十一刚过不久,大家都知道在天猫.京东.苏宁等等电商网站上有很多秒杀活动,例如在某一个时刻抢购一个原价1999现在秒杀价只要999的手机时,会迎来一个用户请求的高峰期,可能会有几十万几百万的 ...
- ubuntu16.04 桌面图标左侧,右侧,底部进行切换
转载:https://jingyan.baidu.com/article/e52e36154e6af340c60c518c.html 传统的 Unity 桌面环境,其应用程序启动器的容器——Launc ...
- thinkphp3.2 控制器导入模型
方法一: public function index(){ $Member = new MemberModel(); $money = $Member->Money(); print_r($mo ...
- WWDC 2015大会到来了
WWDC 2015大会到来了,观看到凌晨3点,困死了. 从现场直播视频可以看到: (1)iOS 9的新体验:Siri更智能.Search更全面.苹果支付更方便.Notes和News更新颖好用.地图应用 ...
- Java多线程-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
Java多线程编程-(1)-线程安全和锁Synchronized概念 Java多线程编程-(2)-可重入锁以及Synchronized的其他基本特性 Java多线程编程-(3)-从一个错误的双重校验锁 ...
- redis sentinels哨兵集群环境配置
# Redis configuration file example. # # Note that in order to read the configuration file, Redis mus ...
- while 1要小心
之前判断一个接口的返回,一定约定好了是返回retcode 1或者retcode 0,就用的这个判断,但是接口挂了的时候,一直返回未登录,找了很长时间为什么cpu一直消耗那么高. 使用wihle 1时候 ...
- ssh跟ssm的区别
SSH跟SSM的区别 SSH指的是:spring+Struts+hibernate:而SSM指的是:spring +SpringMVC + MyBatis. 1.Spring是是开源框架,是轻量级的I ...