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;
}
随机推荐
- 第一個shell腳本
#!/bin/bash echo "Hello World !" 運行
- php base64_encode,serialize对于存入数据表中字段的数据处理方案
A better way to save to Database $toDatabse = base64_encode(serialize($data)); // Save to database $ ...
- java使用javax.mail进行免费的邮件发送
1. 建议发送方使用阿里云邮箱https://mail.aliyun.com/,阿里云默认是开启个人邮箱pop3.smtp协议的,所以无需在阿里云邮箱里设置,pop3.smtp的密码默认邮箱登录密码, ...
- JavaScript高速掌握
.document.write(""); 输出语句 .JS中的凝视为//或/* */ .传统的HTML文档顺序是:document->html->(head,body) ...
- js document.activeElement及使用
// 解决键盘弹出后挡表单的问题 // 解决键盘弹出后挡表单的问题 window.addEventListener('resize', function() { if( document.active ...
- 【伪装位置神器】神行者AnyLocation 1.3.0001可用于微信,陌陌
<ignore_js_op> 软件名称:神行者(破解)软件版本:v1.3.0001授权类别:免费测试机型:大可乐手机 下载链接: http://pan.baidu.com/s/1qWwSM ...
- PHP多文件上传操作
在前一篇文章里讲到了关于PHP文件上传原理和简单操作举例是单文件上传. http://www.cnblogs.com/lichenwei/p/3879566.html 其实多文件上传和单文件上传大同小 ...
- Axure实例演示—登录界面
实例演示 ——登录界面 ...
- 用iostat对linux硬盘IO性能进行检测
近期公司安装了几台DELL PE2650和2850的服务器,统一安装的是RHLE5.132位系统,而服务器的SCSI硬盘都统一做了raid1.公司老总要求对硬盘IO作统一检测报告,在Linux下找了许 ...
- SpringBoot实战(四)之使用JDBC和Spring访问数据库
这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识 H2数据库是一个开源的关系型数据库. H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提 ...