C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式;

它没有更多的更新功能,因此我们这个版本将要完成如下功能:

Problem

1,搜索相关节点;

2,前插节点;

3,后追加节点;

4,一个专门遍历数据的功能;

Solution

我们的数据结构体定义如下,和上一个版本稍微有所不同;

例如,我们把人这个结构体添加一个name域,前几个版本没有名字的节点;

我们叫起来很尴尬,都是第几个第几个节点,好啦!现在有名字啦!

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct person_ {
int hight;
int weight;
char name[];
}Person; typedef struct node_ {
Person *data;
struct node_ * next;
}Node; typedef struct link_list_ {
Node * head;
Node * tail;
int size;
}Link_List;

和第二版本一样,我们需要创建节点的函数;

还需要创建链表的函数;

还有一个插入函数;

Node * create_node(void *data)
{
Node * tmp = (Node*)malloc(sizeof(Node));
if(tmp) {
tmp->data = data;
tmp->next = NULL;
}
return tmp;
} Link_List * init_list(void)
{
Link_List * lst = (Link_List*)malloc(sizeof(Link_List));
lst->head = lst->tail = NULL;
lst->size = ;
return lst;
} void insert(Link_List * lst,void *data)
{
Node * new_node = create_node(data);
if(lst->size == ) {
lst->tail = new_node;
}
new_node->next = lst->head;
lst->head = new_node;
lst->size++;
}

好啦!这些函数都没有注释,第一个原因可以认为我很懒;

第二个原因是可以认为这太简单啦,以至于我们不希望看到多余的注释;

好啦!我们第三版的第一个函数,遍历输出节点的函数:

void travel(Link_List * root)
{
if(root) {
Node * we = root->head;
while(we != NULL) {
printf("Name:%s--Hight:%d--Weight:%d\n",we->data->name,we->data->hight,we->data->weight);
we = we->next;
}
}
return ;
}

我们暂时不测试它先,因为要是测试它,我们留着待会一起测试;

继续,写我们的查找函数,它能够按照给定的名字查找我们的节点哦;

Node * search_by_name(Link_List * lst, char * name)
{
Node * target = lst->head;
while (target != NULL ){
if(!strcmp(target->data->name,name)) {
puts("Found it");
return target;
}
target = target->next;
}
puts("Cannot found it");
return NULL;
}

若是找到啦!会提示我们的,并且返回找的的节点;

当然要是你有同名的节点,它只会把第一个找到得送给你哦;

(这里考虑一下,要是,第一个找到得不是你期望的哪个,你该怎么实现继续往下查找呢)

要是找不到目标节点也会提示我们的,并且返回空指针;

如果成功找到了目标节点,你肯定想看看它长啥样;

所以我们需要一个函数描述它:

void node_detail(Node * node)
{
if(node) {
printf("The detail of target node >>Name: %s -- Hight: %d -- Weight: %d\n",node->data->name,node->data->hight,node->data->weight);
}
return ;
}

有时候我们找到了目标节点,并不是仅仅想看看它长什么样;

还想在它之后添加一个节点;

所以我们需要一个追加函数:

void append_after(Link_List * lst, Node * new, Node * old)
{
Node * target = lst->head;
while(target != NULL){
if(target == old){
new->next = old->next;
old->next = new;
puts("Append successful");
return ;
}
target = target->next;
}
return ;
}

好啦!哪有时候,我们并不想追加,我们想把一个新节点插入到某个节点之前;

该怎么做呢?

注意和前面的追加函数进行对比分析哈!因为他们非常相似;

void insert_before(Link_List *lst, Node * new, Node * old)
{
Node * target = lst->head;
while(target->next != NULL){
if(target->next == old){
new->next = target->next;
target->next = new;
puts("Insert successful");
return ;
}
target->next = target->next->next;
}
return ;
}

好啦!又到我们的测试代码部分啦!

int main(void)
{
Link_List * root = init_list(); Person a = { , , "Frank" };
Person b = { , , "Jack" };
Person c = { , , "Landpack" }; insert(root,&a);
insert(root,&b);
insert(root,&c); //test travel function
travel(root); return ;
}

测试结果:

好啦!继续测试搜索查找节点的功能:

int main(void)
{
Link_List * root = init_list(); Person a = { , , "Frank" };
Person b = { , , "Jack" };
Person c = { , , "Landpack" }; insert(root,&a);
insert(root,&b);
insert(root,&c); //test travel function
travel(root); // test search node
Node * result = search_by_name(root,"Jack");
node_detail(result);
return ;
}

再看看查找结果,并且顺便看看我们的detail函数是否有效;

接下来就是追加函数和插入函数的测试啦;

好啦!首先是追加函数测试;

int main(void)
{
Link_List * root = init_list(); Person a = { , , "Frank" };
Person b = { , , "Jack" };
Person c = { , , "Landpack" }; insert(root,&a);
insert(root,&b);
insert(root,&c); //test travel function
travel(root); // test search node
Node * result = search_by_name(root,"Jack");
node_detail(result); // test append function
Person d = { ,,"Peter" };
Node * new_d = create_node(&d);
append_after(root,new_d,result);
travel(root); return ;
}

测试结果如下:

好啦!最后测试我们的插入函数:

int main(void)
{
Link_List * root = init_list(); Person a = { , , "Frank" };
Person b = { , , "Jack" };
Person c = { , , "Landpack" }; insert(root,&a);
insert(root,&b);
insert(root,&c); //test travel function
travel(root); // test search node
Node * result = search_by_name(root,"Jack");
node_detail(result); // test append function
Person d = { ,,"Peter" };
Node * new_d = create_node(&d);
append_after(root,new_d,result);
travel(root); // test insert function
Person e = { ,,"Sap" };
Node * new_e = create_node(&e);
insert_before(root,new_e,result);
travel(root); return ;
}

测试结果如下:

好啦!我们本次版本更新的所有的任务完成啦!

要是项目经理满意的话会请我们喝啤酒啦!

所有代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct person_ {
int hight;
int weight;
char name[];
}Person; typedef struct node_ {
Person *data;
struct node_ * next;
}Node; typedef struct link_list_ {
Node * head;
Node * tail;
int size;
}Link_List; Node * create_node(void *data)
{
Node * tmp = (Node*)malloc(sizeof(Node));
if(tmp) {
tmp->data = data;
tmp->next = NULL;
}
return tmp;
} Link_List * init_list(void)
{
Link_List * lst = (Link_List*)malloc(sizeof(Link_List));
lst->head = lst->tail = NULL;
lst->size = ;
return lst;
} void insert(Link_List * lst,void *data)
{
Node * new_node = create_node(data);
if(lst->size == ) {
lst->tail = new_node;
}
new_node->next = lst->head;
lst->head = new_node;
lst->size++;
}
void travel(Link_List * root)
{
if(root) {
Node * we = root->head;
while(we != NULL) {
printf("Name:%s--Hight:%d--Weight:%d\n",we->data->name,we->data->hight,we->data->weight);
we = we->next;
}
}
return ;
} Node * search_by_name(Link_List * lst, char * name)
{
Node * target = lst->head;
while (target != NULL ){
if(!strcmp(target->data->name,name)) {
puts("Found it");
return target;
}
target = target->next;
}
puts("Cannot found it");
return NULL;
} void node_detail(Node * node)
{
if(node) {
printf("The detail of target node >>Name: %s -- Hight: %d -- Weight: %d\n",node->data->name,node->data->hight,node->data->weight);
}
return ;
} void append_after(Link_List * lst, Node * new,Node * old)
{
Node * target = lst->head;
while(target != NULL){
if(target == old){
new->next = old->next;
old->next = new;
puts("Append successful");
return ;
}
target = target->next;
}
return ;
}
void insert_before(Link_List *lst, Node * new, Node * old)
{
Node * target = lst->head;
while(target->next != NULL){
if(target->next == old){
new->next = target->next;
target->next = new;
puts("Insert successful");
return ;
}
target->next = target->next->next;
}
return ;
} int main(void)
{
Link_List * root = init_list(); Person a = { , , "Frank" };
Person b = { , , "Jack" };
Person c = { , , "Landpack" }; insert(root,&a);
insert(root,&b);
insert(root,&c); //test travel function
travel(root); // test search node
Node * result = search_by_name(root,"Jack");
node_detail(result); // test append function
Person d = { ,,"Peter" };
Node * new_d = create_node(&d);
append_after(root,new_d,result);
travel(root); // test insert function
Person e = { ,,"Sap" };
Node * new_e = create_node(&e);
insert_before(root,new_e,result);
travel(root); return ;
}

Discussion

我们的链表似乎挺棒啦!但是烦人的项目经理是不会满足的;

他说下次版本更新,最好添加一个删除节点的功能;

(因为他可能想解聘一些员工啦!希望不会是我哦)

还有。。。。

他说反正有很大任务的。。。。

哈哈下次见;

最新文章

  1. 记录一次bug解决过程:可维护性和性能优化
  2. tiltShift.js - CSS3 滤镜实现移轴镜头效果
  3. qml基础学习 模型视图(一)
  4. linux之V4L2摄像头应用流程【转】
  5. 配置apache以fastcgi运行php
  6. JVM基础知识总结
  7. 使用openssl库实现des,3des加密
  8. placeholder属性兼容js支持
  9. Bash扩展顺序
  10. 好代码是管出来的——浅谈.Net Core的代码管理方法与落地(更新中...)
  11. [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List
  12. leetcode — word-ladder
  13. dubbo常见面试问题(二)
  14. 【机器学习】异常检测算法(I)
  15. python 面向对象(一)初识面向对象
  16. 练就Java24章真经—你所不知道的工厂方法
  17. 策略模式(Strategy )
  18. CCF2016093炉石传说(C语言版)
  19. iOS-原生纯代码约束总结(二)之 AutoLayout
  20. ubuntu下安装软件的三种方法

热门文章

  1. hibernate学习(设计多对多 关系 映射)
  2. okhttp-utils的封装之okhttp的使用
  3. Filter过滤器简单应用( 接口访问控制 )
  4. 第一章 tomcat安装与启动
  5. B. Factory Repairs--cf627B(线段树)
  6. KMP算法(快速模式匹配)
  7. java设计模式之外观模式(门面模式)
  8. 黑马程序员_Java基础:多功能小窗口,swing,io,net综合应用
  9. opengles2.0之图元装配和光栅化
  10. MySQL中表名大小写问题