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;
}
随机推荐
- bioperl 自动化下载genbank 中的序列
当我们想要从genbank 中下载序列的时候,总需要点击右上角的download 按钮,选择对应的格式,然后通过浏览器进行下载,这样反复的点击很费时间了 其实可以通过bioperl 自动化的完成下载: ...
- YII2 搭建redis拓展
安装redis扩展: 1.通过composer进行安装,到项目根目录cmd运行(推荐) php composer.phar require --prefer-dist yiisoft/yii2-red ...
- glsl boom
原理: 1.渲染场景到fbo 2.对fbo进行高斯横向,纵向模糊,到新的fbo 3.对两张图进行叠加 模糊后的 效果就这样 给数据加大 <-vertex-> #version varyin ...
- 避免使用jQuery的html方法来替换标签,而是使用replaceWith方法
tblCostSplit.html内容: <nobr title="">只想显示里面内容,去掉nobr标签</nobr> <nobr title=&q ...
- git远端删除被提交后又被加到.gitignore的文件
远端删除文件而不影响本地文件 git rm [-r] --cached file_or_dir_name 利用.gitignore来自动删除所有匹配文件 我试过网上推荐的写法 git rm --cac ...
- vuex在页面中以对象展开运算符形式引入报错解决
当页面中以mapGetters等对象扩展运算符写的时候,会报错 unexpected token 解决如下: babel还要加插件才能解释这个写法 npm install --save-dev bab ...
- QT编译错误:member access into incomplete type 'QMouseEvent'
想在QT程序中使用鼠标事件,添加重载的响应函数,并实现后,一直提示 member access into incomplete type 'QMouseEvent' 既然使用了QMouseEvent类 ...
- MTK 隐藏上方的状态栏
步骤一: 源码/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.ja ...
- Okhttp封装、网络层扩展
一.概述 首先在这里本片文章是以网络通信封装为主,而app开发首先重要就是网络通信,而如今主流的async.volley.okhttp等,阿么这么网络库怎样能做到更好封装.更好的切换,从而不影响业务层 ...
- Java实现文件批量重命名
Windows操作系统可以实现重命名文件操作,却不能实现批量重命名.本实例实现了批量重命名功能,可以将一个文件夹内同一类型的文件按照一定的规则批量重命名.用户可以给出重命名模板,程序可以根据模板对相应 ...