有序链表插入新结点的改进
2011-02-23 21:59
351 查看
#include <stdlib.h> #include <stdio.h> #define FALSE 0 #define TRUE 1 typedef struct p { struct p *link; int val; } *pNode, node; int insert(register pNode *pLink, int new_val); int main() { pNode root = NULL; pNode *pLink = &root; int i; for (i = 0; i < 50; i+=3) insert(pLink, i); insert(pLink, 50); insert(pLink, 31); insert(pLink, -1); pNode p = root; while (p != NULL) { printf("%d ", p->val); p = p->link; } printf("/n"); return 0; } int insert(register pNode *pLink, int new_val) { register pNode current; register pNode pNewNode; while ((current = *pLink) != NULL && current->val < new_val) pLink = ¤t->link; pNewNode = (pNode)malloc(sizeof(node)); if (pNewNode == NULL) return FALSE; pNewNode->val = new_val; pNewNode->link = current; *pLink = pNewNode; return TRUE; }
相关文章推荐
- 第十八周项目一(6):将值为x的结点插入到建立起来的有序链表中
- 向头指针为 L的有序单链表(从小到大有序)中插入一个结点,使插入后链表仍然有序
- 在一个递增有序链表中插入新结点并保持链表递增顺序不变
- 链表(三)——链表删除冗余结点&插入结点到有序链表
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 数据结构与算法分析笔记与总结(java实现)--链表9:有序环形链表中插入新结点问题
- 【数据结构作业二】写出单链表结点的结构体类型定义及查找、插入、删除算法,并以单链表作存储结构,实现有序表的合并
- 在有序链表中插入结点
- 链表(三)——链表删除冗余结点&插入结点到有序链表
- 有序的循环链表中插入结点
- 从键盘读入n个整数(升序),请编写算法实现: 建立带表头结点的单链表; 显示单链表,(形如:H->10->20->30->40); 在有序单链表中插入新的数据元素x; 将单链表就地逆
- 链表1 ----- 链表的基本创建以及有序插入的思想
- 单链表有序插入
- 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
- 将节点插入到建立起来的有序链表中
- 有序链表的插入
- java数据结构-链表实现堆栈 控制插入的方式,成为有序链表
- 在链表中插入和删除结点
- 链表——结点的插入
- 有序插入时,数组与链表效率比较