码迷,mamicode.com
首页 > 其他好文 > 详细

单链表的插入操作

时间:2014-11-25 18:32:18      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:单链表插入

从C和指针这本书中学到的优化插入函数写法:

#include<stdio.h>
typedef struct Node{
    int value;
struct  Node*link;
}Node;
int sll_insert(Node **linkp,int new_value){
    Node *current=*linkp;
    while(current!=NULL&&current->value<new_value)
    {
        linkp=¤t->link;
        current=current->link;
    }
    Node* new=(Node*)malloc(sizeof(Node));
    new->value=new_value;
    new->link=current;
    *linkp=new;
}
void pr(Node*p){
    while(p!=NULL)
    {
        printf("%d ",p->value);
        p=p->link;
    }
}

int main(int argc, char *argv[])
{
    int i=10;
    Node *root;
    for(;i>=0;--i)
        sll_insert(&root, i);
    pr(root);
    return 0;
}
输出结果如下:

bubuko.com,布布扣

单链表的插入操作

标签:单链表插入

原文地址:http://blog.csdn.net/wdkirchhoff/article/details/41485539

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!