单链表的插入
2015-07-09 22:33
267 查看
预编译
数据结构
插入代码
#include <stdio.h> #include <stdlib.h> #define status int #define TRUE 1 #define FALSE 0
数据结构
typedef struct NODE{ struct NODE *next; /* 指向下一个结点 */ int value; /* 存放结点值 */ }Node, *PNode, **List;
插入代码
/* 功能描述: 把一个值插入到单链表(从小到大) 参数: rootp -- 指向根结点 new_value -- 存放新值 返回值: 如果插入成功,返回TRUE;否则,返回FALSE */ status Insert( List linkp, int new_value ) { if ( linkp == NULL ){ return FALSE; } Node *current = NULL; /* 寻找插入位置 */ while ( (current = *linkp) != NULL && current->value <= new_value ){ if ( current->value == new_value ){ printf( "%d已存在\n", new_value ); return FALSE; } linkp = ¤t->next; } /* 创建新结点 */ Node *_new = (PNode) malloc ( sizeof( Node ) ); if ( _new == NULL ){ printf( "内存不足\n" ); return FALSE; } _new->value = new_value; /* 插入新结点 */ _new->next = current; *linkp = _new; return TRUE; }
相关文章推荐
- 怎样在Xcode7上面去禁止iOS 9 的 App 传输安全 ,以及修复“Could not connect to sever”错误
- SpringMVC中使用aop注解无效的问题
- [图像]图像灰度均衡化和灰度直方图(Matlab实现)
- 如何创建微信公众号, 微网站 以及发多图文消息
- Application
- LaTeX技巧211:latex如何打印出确切时间
- 1.Two Sum (Array; Divide-and-Conquer)
- android AsyncTask和Handler对比
- [Paper]科技论文写作要点
- Hibernate学习笔记之EHCache的配置
- 机器学习中的相似性度量
- C#占位符
- 习题3.11 递归和非递归查找元素
- spring MVC环境搭建
- python入门知识
- Oracle数据库的基本概念及PL/SQL(DDL)
- 20150708 MyEclipse Hibernate Many To Many Mapping
- JAVA基础之内部类一
- 算法之美 之 小小方差增量算法带来的大大收益
- c语言第二周总结-指针