数据结构 -- 单链表
2014-01-13 17:00
113 查看
快要放假了,实在是待不住了,论文也看不下去,也没啥其他的事做,就写写常用的数据结构吧,正好下个学期就要找工作了,也是方便以后自己使用这些数据结构,好吧,这里实现的数据结构没有太多的错误控制,需要自己去判断什么不应该为空,第一篇文章贴出最经典的单链表的实现:
头文件:
C文件:
头文件:
/* * dlut_list.h * * Created on: 2014年1月13日 * Author: DLUTBruceZhang */ #ifndef DLUT_LIST_H_ #define DLUT_LIST_H_ typedef int need; typedef struct _list { need data; struct _list *next; }list; list * dlut_list_create(void); void dlut_list_insert_to_head(list *, need); void dlut_list_insert_to_tail(list *, need); void dlut_list_delete_the_head(list *); void dlut_list_delete_the_tail(list *); void dlut_list_delete_the_data(list *, need); need dlut_list_get_the_head(list *); need dlut_list_get_the_tail(list *); void dlut_list_print_the_list(list *); void dlut_list_delete_the_list(list *); #endif /* DLUT_LIST_H_ */
C文件:
/* * dlut_list.c * * Created on: 2014年1月13日 * Author: DLUTBruceZhang */ #include <stdio.h> #include <stdlib.h> #include "dlut_list.h" list *dlut_list_create() { list *head; head = (list *)malloc(sizeof(list)); if (!head) return NULL; head -> data = 0; head -> next = NULL; return head; } void dlut_list_insert_to_head(list *head, need data) { list *new_node; new_node = (list *)malloc(sizeof(list)); if (!new_node) return; new_node -> next = head -> next; new_node -> data = data; head -> next = new_node; head -> data += 1; return; } void dlut_list_insert_to_tail(list *head, need data) { list *_head = head; list *new_node; new_node = (list *)malloc(sizeof(list)); if (!new_node) return; while (_head -> next != NULL) _head = _head -> next; new_node -> data = data; new_node -> next = NULL; _head -> next = new_node; head -> data += 1; return; } void dlut_list_delete_the_head(list *head) { if (head -> next == NULL) return; list *old_node = head -> next; head -> next = head -> next -> next; head -> data -= 1; free(old_node); return; } void dlut_list_delete_the_tail(list *head) { if (head -> next == NULL) return; list *_head = head; while (_head -> next -> next != NULL) _head = _head -> next; list *old_node = _head -> next; _head -> next = NULL; head -> data -= 1; free(old_node); return; } void dlut_list_delete_the_data(list *head, need data) { if (head -> next == NULL) return; list *_head = head; while (_head -> next != NULL && _head -> next -> data != data) _head = _head -> next; if (!_head -> next) return; list *old_node = _head -> next; _head -> next = _head -> next -> next; head -> data -= 1; free(old_node); return; } need dlut_list_get_the_head(list *head) { return head -> next ? head -> next -> data : -1; } need dlut_list_get_the_tail(list *head) { if (head -> next == NULL) return -1; while (head -> next != NULL) head = head -> next; return head -> data; } void dlut_list_print_the_list(list *head) { while (head -> next != NULL) { printf("%d ", head -> next -> data); head = head -> next; } printf("\n"); return; } void dlut_list_delete_the_list(list *head) { while (head -> next != NULL) { dlut_list_delete_the_head(head); } free(head); return; }
相关文章推荐
- [数据结构]线性表之单链表的类模板实现
- 《数据结构》 单链表常用操作代码集合
- 数据结构复习笔记(数据类型及单链表实现)
- 数据结构(14)单链表
- 一步一步复习数据结构和算法基础-单链表冒泡排序
- 数据结构 PAT 02-线性结构2 Reversing Linked List 单链表实现
- 数据结构 单链表
- 数据结构单链表的实现以及相关操作
- 数据结构学习笔记(二) 链表之单链表的基本操作
- 【C++研发面试笔记】7. 基本数据结构-单链表
- 数据结构(三)——单链表的插入与删除
- 数据结构(二)——单链表的概念和读取元素
- 数据结构_单链表
- 线性表之单链表学习小结(初学数据结构必看)
- 【java数据结构】单链表实现
- 数据结构【线性表(二)链表】项目之自建算法库—单链表
- 数据结构----单链表的创建、插入、删除、读取、遍历
- 【算法数据结构Java实现】Java实现单链表
- 数据结构(C#):单链表
- 【数据结构-链表】头插法和尾插法建立单链表