数据结构(三) -- 单向链表
2016-07-31 12:06
357 查看
简介
单向链表是链表的一种,它是单一方向的,访问时只能从头节点开始。单向链表实现
在实现单向链表时,最好加入头节点的概念。头节点(head)不存储数据,它的下一个节点才是真正存储数据的节点,这样的做的好处是可以使头节点保持不变,令对链表第一个元素(头节点的下一个元素)的操作与对链表其他元素的操作保持一致,简化代码逻辑。<list.h>
#ifndef __LIST_H__ #define __LIST_H__ typedef struct node { int data; struct node *next; }Node; typedef struct list { Node *head; int length; }List; /* 创建并返回链表 */ List *createList(void); /* 销毁链表 */ void destroyList(List *list); /* 链表是否为空 */ int listIsEmpty(List *list); /* 获取链表长度*/ int getListLength(List *list); /* 获取链表positon位置节点的数据*/ int listGet(List *list, int position); /* 在链表positon位置节点后插入一个节点*/ int listInsert(List *list, int data, int position); /* 删除链表positon位置的节点*/ int listDelete(List *list, int position); /* 遍历当前链表,打印数据*/ void listTraverse(List *list); #endif
<list.c>
#include <stdio.h> #include <stdlib.h> #include "list.h" List *createList(void) { List *list; list = (List *)malloc(sizeof(List)); if(NULL == list) { return NULL; } list->head = (Node *)malloc(sizeof(Node)); if(NULL == list->head) { free(list); return NULL; } list->head->data = 0; list->head->next = NULL; list->length = 0; return list; } void destroyList(List *list) { Node *cur, *tmp; if(list == NULL) { return; } cur = list->head; while(cur != NULL) { tmp = cur->next; free(cur); cur = tmp; } free(list); list = NULL; } int listIsEmpty(List *list) { if(list->length == 0) { return 1; } return 0; } /** * 插入数据到postion位置的后边 * param list 链表指针 * param data 插入的数据 * param postion 插入数据的位置, 0表示在在头节点后插入 */ int listInsert(List *list, int data, int position) { Node *cur; Node *newNode; int i = 0; if(position < 0 || position > list->length) { printf("insert node position:%d is invalid\n", position); return -1; } newNode = (Node *)malloc(sizeof(Node)); if(newNode == NULL) { return -1; } newNode->data = data; cur = list->head; for(i = 0; i < position; i++) { //找到position位置的节点 cur = cur->next; } newNode->next = cur->next; cur->next = newNode; list->length++; return 0; } int listDelete(List *list, int position) { int i; Node *cur, *del; if(list->length == 0) { printf("list is Empty\n"); return -1; } if(position < 1 || position > list->length) { //position不能为0,因为不能删除头节点 printf("delete node position:%d is invalid\n", position); return -1; } cur = list->head; for(i = 0; i < position - 1; i++) { //找到要删除节点的前一个节点 cur = cur->next; } del = cur->next; cur->next = del->next; free(del); del = NULL; list->length--; return 0; } int listGet(List *list, int position) { int i; Node *cur; if(list->length == 0) { printf("list is Empty\n"); return -1; } if(position < 1 || position > list->length) { //position不能为0,头节点没有数据 printf("insert node position:%d is invalid\n", position); return -1; } cur = list->head; for(i = 0; i < position; i++) { //找到position位置的节点 cur = cur->next; } return cur->data; } void listTraverse(List *list) { Node *cur; for(cur = list->head->next; cur != NULL; cur = cur->next) { printf("[%s] %d\n", __FUNCTION__, cur->data); } }
github地址(含Makefile和测试代码) https://github.com/zsirkg/myWorks/tree/master/algorithms_and_data_structures/list/single
相关文章推荐
- 数据结构——单向链表操作
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 数据结构之单向循环链表
- I学霸官方免费教程三十七:Java数据结构之单向链表结构
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 数据结构与算法 2、单向/双向链表
- 数据结构之—线性表之—链表之—1.单向链表
- 数据结构之单向链表
- 数据结构之设置尾指针的单向循环链表(参考整理严蔚敏数据结构)
- 数据结构之单向链表实现
- 数据结构——单向链表的顺序存储及实现
- 手把手写数据结构之单向链表操作(二)
- 数据结构中之单向循环链表的使用
- [数据结构算法学习] 单向链表
- 数据结构之链表与数组(二) -单向链表上的简单操作问题
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 单向链表实现多项式加和乘--自己写数据结构
- c语言_数据结构_单向循环链表