创建单向链表
2017-06-30 17:12
127 查看
算法描述1:正向创建单向链表(尾插方式)
算法描述2:反向创建单向链表(头插方式)
算法描述3:递归的方式创建单向链表日后会补上的
//创建链表节点 typedef struct node { int data; struct node *next; }ElemSN; //创建单向链表 ElemSN* CreatLink(int a[], int n) { int i; ElemSN *ptail = NULL, *p = NULL, *h = NULL; for (i = 0; i < n; i++) { p = (ElemSN*)malloc(sizeof(ElemSN)); //初始化数据域,初始化指针域 p->data = a[i]; p->next = NULL; if (!h)//判断是否有头结点没有,创建头结点 { h = p; ptail = h; } else { ptail->next = p;//挂链 ptail = ptail->next;//挪尾指针 } } return h; }
算法描述2:反向创建单向链表(头插方式)
//创建节点方式与正向建链相同 ElemSN* CreatLink(int a[], int n) { ElemSN *p = NULL, *h = NULL; for (int i = n; i > 0; i--) { p = (ElemSN*)malloc(sizeof(ElemSN)); //初始化指针域,数据域 p->next = NULL; p->data = a[i - 1]; p->next = h;//头插 h = p;//挪头指针 } return h; }
算法描述3:递归的方式创建单向链表日后会补上的
这里写代码片
相关文章推荐
- C语言创建单向链表之逆向建链
- 单向链表->创建节点
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- 单向循环链表的创建/插入/删除/输出算法
- 关于单向链表创建以及一些简单的增删查改功能
- C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。
- 单向链表的创建和遍历
- C语言数据结构(链表)单向链表的创建删除逆序
- 单向链表的创建及翻转
- 线性表之单向链表的创建、插入、删除和清除
- java实现单向链表--创建、遍历
- 单向链表的创建与遍历(先进先出和先进后出)
- 单向链表的创建、节点删除、链表输出、翻转(写法参考计蒜客数据结构课程)
- linux 下c语言创建单向动态链表的理解
- 单向链表的创建、遍历、求长、判存、判空、插入、删除、查找(按位置或元素)、合并
- 利用类对象为节点创建简单单向链表
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- 创建一个简单的单向链表并将其倒序输出
- 单向链表的创建 输入 输出
- 单向链表(1)-创建