您的位置:首页 > 理论基础 > 数据结构算法

[数据结构]顺序单链表插入

2015-01-20 20:09 225 查看
一,单链表插入操作

[cpp] view plaincopy





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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: