c++单向链表-数据结构
2017-02-02 20:52
399 查看
单向链表看着简单,实现起来还是需要很仔细,指向的元素需要想清楚,内存需要能正确释放,代码要讲究可读性,试了一下午,以下是自定义的实现:
//list.h
//list.cpp
//测试一下:
//list.h
#ifndef LIST_H_H_ #define LIST_H_H_ #include<cstdio> #include<cstdlib> #include<cassert> /** 节点中的具体的数据类型 */ typedef int DataType; /** 链表中的节点 */ struct Node { DataType data_; Node* next; }; /** 表头 */ struct List { Node* head; ///<指向链表的第一个节点 }; /** 初始化链表 */ void list_init(List* list);; /** 插入数据(在链表头部) */ int list_insert(List* list, DataType data); /** 在指定的元素后面插入数据 */ int list_insert(List* list, DataType element, DataType data); /** 删除节点 */ int list_remove(List* list, DataType data); /** 寻找节点,返回找到的节点或NULL */ Node* list_find(List* list, DataType data); /** 链表大小 */ int list_size(List* list); /** 删除链表(不含表头) */ void list_destroy(List* list);; #endif // !LIST_H_H_
//list.cpp
#include "list.h" /** 初始化链表 */ void list_init(List * list) { list->head = NULL; } /** 插入数据(在链表头部) */ int list_insert(List * list, DataType data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { fprintf(stderr, "out of memory"); return -1; } newNode->data_ = data; newNode->next = list->head; list->head = newNode; return 0; } /** 在指定的元素后面插入数据 */ int list_insert(List * list, DataType element, DataType data) { Node* p = list_find(list, element); if (p == NULL) { fprintf(stderr, "can't find the element"); return -1; } Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { fprintf(stderr, "out of memory"); return -1; } newNode->data_ = data; newNode->next = p->next; p->next = newNode; return 0; } /** 删除节点 */ int list_remove(List * list, DataType data) { Node* tempNode; Node* p = list->head; //是否是空链表 if (p == NULL) { return -1; } //如果是第一个节点 if (p->data_ == data) { tempNode = p->next; #ifdef _DEBUG //test code printf("node remove:%d\n", p->data_); #endif // _DEBUG free(p); list->head = tempNode; return 0; } else { //遍历下一个节点 while ((p->next != NULL) && (p->next->data_ != data)) { p = p->next; } if (p->next==NULL) { fprintf(stderr, "not found element at %s\n",__FUNCTION__); return -1; } else { tempNode = p->next->next; #ifdef _DEBUG //test code printf("node remove:%d\n", p->next->data_); #endif // _DEBUG free(p->next); p->next = tempNode; return 0; } } } /** 寻找节点,返回找到的节点或NULL */ Node * list_find(List * list, DataType data) { Node* p = list->head; while ((p != NULL) && (p->data_ != data)) { p = p->next; } return p; } /** 链表大小 */ int list_size(List * list) { Node* p = list->head; int count = 0; while (p != NULL) { count++; p = p->next; } return count; } /** 删除链表(不含表头) */ void list_destroy(List * list) { Node* tempNode; Node* p = list->head; while (p != NULL) { tempNode = p->next; #ifdef _DEBUG //test code printf("node destroy:%d\n", p->data_); #endif // _DEBUG free(p); p = tempNode; } list->head = NULL; }
//测试一下:
#include<iostream> #include"list.h" int main() { List list; list_init(&list); list_insert(&list, 3); list_insert(&list, 2); list_insert(&list, 1); list_insert(&list, 5); list_insert(&list, 1, 7); list_remove(&list, 1); Node* first = list.head; while (first!=NULL) { std::cout << first->data_ << std::endl; first = first->next; } list_remove(&list, 9); Node* f1=list_find(&list,2); if (f1) { std::cout << "find:" << f1->data_ << std::endl; } else { std::cout << "not find" << std::endl; } Node* f2 = list_find(&list, 22); if (f2) { std::cout << "find:" << f2->data_ << std::endl; } else { std::cout <<"not find"<< std::endl; } std::cout << "list size--------------:" << list_size(&list) << std::endl; list_destroy(&list); std::cout << "list size after destroy:" << list_size(&list) << std::endl; return 0; }
相关文章推荐
- 数据结构之单向链表的实现【C++】
- 数据结构基础:c++实现单向链表
- c++数据结构之链表
- php学习第一章:PHP基础语法(三)数据结构与算法:2、单向链表
- 手把手写数据结构之单向循环链表操作
- 第十八节 数据结构之单向链表
- 软件设计师教程 数据结构之线性链表的实现 (C/C++语言)
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 数据结构(3)单循环链表 c++ 模板实现
- 数据结构之链表单向操作总结
- 数据结构单项链表C++实现改变C版本
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- (摘)面向对象c++数据结构之基本数据结构-链表-使用链表管理器(连载中)
- [数据结构算法学习] 单向链表
- 数据结构(java)——单向链表
- 数据结构之单循环链表C++(模板)
- 数据结构之链表的实现-------C++课程设计-----学生选课管理系统
- 数据结构——单向链表的顺序存储及实现
- 手把手写数据结构之单向链表操作(二)