[数据结构]顺序单链表插入
2015-01-20 20:09
225 查看
一,单链表插入操作
[cpp] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
typedef struct NODE {
struct NODE *link;
int value;
}Node;
#include <stdlib.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int s_insert(Node **rootp,int new_value) //root是一个指向Node的指针,所以指针root的指针类型是Node**
{
Node *current;
Node *new;
Node *previous;
current = *rootp;
previous = NULL;
while(current != NULL && current->value < new_value)
{
previous = current;
current = current->link;
}
new = (Node*)malloc(sizeof(Node));
if(new == NULL)
{
return FALSE;
}
new->value = new_value;
new->link = current;
if(previous == NULL)
{
*rootp = new;
}
else
{
previous->link = new;
}
return TRUE;
}
[cpp] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
typedef struct NODE {
struct NODE *link;
int value;
}Node;
#include <stdlib.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int s_insert(Node **rootp,int new_value) //root是一个指向Node的指针,所以指针root的指针类型是Node**
{
Node *current;
Node *new;
Node *previous;
current = *rootp;
previous = NULL;
while(current != NULL && current->value < new_value)
{
previous = current;
current = current->link;
}
new = (Node*)malloc(sizeof(Node));
if(new == NULL)
{
return FALSE;
}
new->value = new_value;
new->link = current;
if(previous == NULL)
{
*rootp = new;
}
else
{
previous->link = new;
}
return TRUE;
}
相关文章推荐
- [数据结构]顺序单链表插入
- 【数据结构】顺序表、单链表、循环链表的插入与删除
- 【程序员面试宝典】数据结构基础二单链表循环链表的操作 建立|插入删除|打印|合并
- 数据结构—顺序表的插入算法
- 数据结构实验之链表一:顺序建立链表
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- 山东理工大学oj【2116】数据结构实验之链表一:顺序建立链表
- 数据结构之双向链表,头部插入数据
- 数据结构之顺序链表
- 数据结构——单链表的创建、逆置、插入、有序表的建立、有序单链表合并等基础操作!!
- 数据结构之链表的插入
- 数据结构之顺序表,插入,删除等
- 实现两个链表的合并 可变长顺序表设计 数据结构
- 求指教,数据结构(双链表插入)
- 程序员面试宝典之数据结构基础----⑥双链表的建立,插入和删除
- 【数据结构】顺序队列与链表队列
- Java单链表顺序和链式实现(数据结构五)
- 算法学习-数据结构之链表操作,创建,插入,删除,查找。
- 山东理工大学oj【2116】数据结构实验之链表一:顺序建立链表
- 数据结构实验之链表一:顺序建立链表