c语言实现单链表

1. 单链表
  头节点,下一个节点,以线性方式串联一起,此种结构的删除和增加数据的效率高,查询效率需要按节点一个一个的遍历,按线性查找。
  a.头文件 slinkh.h  
C/C++ code
#define TRUE 1 #define FALSE 0 typedef struct NODE { struct NODE *link; char *name; }Node; /* ** 创建一个节点 ** */ Node* create_node(); /* ** 打印一个函数的节点所带信息 ** */ void printf_node(Node *head,char *name); /* ** 释放内存 回收 */ void free_node(Node *head,char *name); /* ** 插入节点 ** */ int insert_node(Node *head,char *name); /* ** 删除节点 释放内存 */ int dele_node(Node *head,char *name); /* ** 遍历所有节点 采用回调函数的方式 处理数据 ** */ void foreach_node(Node *head,void (*fuction)(Node *curr, char *name)); /* ** 查找节点 */ Node* search_list(Node *node,char *name);

------------------------------------------------------------slinkh.h  
  b.具体实现 slink.c  
C/C++ code
#include "slinkh.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define NAME_LENGTH 10 #define FALSE 0 #define TRUE 1 /* ** author:srgzyq ** email:srgzyq@gmail.com */ /* ** 创建一个节点实现 支持的name字符的长队为10 ** */ Node* create_node() { Node *new = (Node *)malloc(sizeof(Node)); if(new == NULL) return NULL; // 开辟数组 char *name = (char *)calloc(NAME_LENGTH,sizeof(char)); if(!name) { free((Node *)new); return NULL; } new->name = name; return new; } void printf_node(Node *pNode,char *name) { printf("name: %s\n",pNode->name); } void free_node(Node *pNode, char *name) { Node *curNode = pNode; free((Node *)curNode); printf("free node: %s\n",name); } /* ** 遍历所有节点 ** @param head 头节点 ** @param fuction 为回调函数 */ void foreach_node(Node *head,void (*fuction)(Node *curr,char *name)) { Node *currNode = head->link; Node *nextNode; while(currNode != NULL) { nextNode = currNode->link; fuction(currNode,currNode->name); currNode = nextNode; } } /** ** 插入函数没有走foreach_node回调,主要是自己懒,不想改了 */ int insert_node(Node *head,char *name) { Node *currNode = head->link; Node *preNode = head; Node *new = create_node(); if(new == NULL) return FALSE; new->name = name; while((currNode != NULL && compare_name(currNode->name,name) < 0)) { preNode = currNode; currNode = currNode->link; } preNode->link = new; new->link = currNode; return TRUE; } /* ** 删除函数没有走foreach_node回调,主要是自己懒,不想改了 ** */ int dele_node(Node *head,char *name) { Node *currNode = head->link; Node *preNode = head; while(currNode != NULL && compare_name(currNode->name,name) != 0) { preNode = currNode; currNode = currNode->link; } if(currNode != NULL) { preNode->link = currNode->link; free_node(currNode,currNode->name); } return TRUE; } int compare_name(char const *name,char const *key_name) { return strcmp(name,key_name); } Node* search_list(Node *node,char *name) { node = node->link; while(node != NULL) { if(compare_name(node->name,name) == 0) break; node = node->link; } return node; }

--------------------------------------------------------slink.c  
  c.测试代码slinktest.c (注:用了兄弟伙的名字)
C/C++ code
#include "slinkh.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define ARR_LEN 5 char data_arr[][10] = {"wangbo","raodie","niba","bobo","pengdui"}; int main() { Node *head = create_node(); head->link = NULL; int index; for(index = 0; index <ARR_LEN; index++) { insert_node(head,data_arr[index]); } dele_node(head,data_arr[1]); foreach_node(head,printf_node); Node *keynode = search_list(head,data_arr[4]); if(keynode != NULL) printf_node(keynode,keynode->name); foreach_node(head,free_node); return EXIT_SUCCESS; }

---------------------------------------------------------------------------slinktest.c  

posted on 2011-06-30 09:39  风乔  阅读(255)  评论(0编辑  收藏  举报

导航