您的位置:首页 > 其它

有序链表插入新结点的改进

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;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  insert struct null include
相关文章推荐