题解 | #牛牛的双链表求和#

牛牛的双链表求和

http://www.nowcoder.com/practice/efb8a1fe3d1f439691e326326f8f8c95

#include <stdlib.h>

//单向循环链表
typedef struct node
{
    int data;
    struct node *next;
}node;

//创建链表b头节点
node *add_head()
{
    node *Head = (node *)malloc(sizeof(node));
    if(Head == NULL)
        return NULL;
    Head->next = Head;
    return Head;     
}

//尾插法
void add_node(node *Head,int data)
{
    node *new = (node*)malloc(sizeof(node));
    if(new == NULL)
        return;
    //节点成员赋值
    new->data = data;
    new->next = NULL;
    //链接
    
        node *pT = NULL;
        for(pT = Head;pT->next != Head;pT = pT->next);
        
        new->next = pT->next;
        pT->next = new;
}



//输出链表
void output(node *Head)
{
    if(Head->next == Head)
        return;
    node *pT = Head->next;
    while(pT != Head)
    {
        printf("%d ",pT->data);
        pT = pT->next;
    }
}

int main(void)
{
    node *Head = add_head();//链表头节点
    int n,i,j;
    scanf("%d",&n);
    int arr[n];
    int brr[n];
    
    //将键盘键入的数据存放到数组中
    for(i = 0;i < n;i++)
        scanf("%d",&arr[i]);
    for(i = 0;i < n;i++)
        scanf("%d",&brr[i]);
    //将数据插入链表
    for(j = 0;j < n;j++)
        add_node(Head, arr[j]+brr[j]);
    
    
    output(Head);
    
    return 0;
}

全部评论

相关推荐

05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
07-18 15:02
门头沟学院 Java
刚打开网申页面就不想填了,还是不要为难自己了
poppinzhan...:多益老行业毒瘤了,碰到徐波这种恶心的烂人,去了也是受罪。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务