| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1027 人关注过本帖
标题:链表节点插入
只看楼主 加入收藏
chenhaiyuan
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2018-4-16
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
链表节点插入
编译后提示下方注释出:可能使用了未初始化本地变量指针pt,但是在while循环中不是有定义吗?
要如何让修改?

#include<stdio.h>
struct employee
{
        int no;
        char name[20];
        struct employee *next;
};
struct employee *insert(struct employee *head, struct employee *pb)
{
        struct employee *pn, *pt;
        pn = head;
        if (pn == NULL)
        {
                head = pb;
                pb->next = NULL;
        }                                                                          
        else
        {
                while ((pn->no != pb->no)&&(pb->next != NULL))                           
                {
                        pt = pn;
                        pn = pn->next;
                }
                if (pn->no == pb->no)
                {
                        if (head == pn)
                                head = pb;
                        else
                                pt->next = pb;                                          //???????
                        pb->next = pn;
                }
                else
                {
                        pn->next = pb;
                        pb->next = NULL;
                }
        }
        return head;
搜索更多相关主题的帖子: struct employee next head pb 
2018-04-16 11:05
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:10 
随便整点数据测试的
#include <stdio.h>

struct employee
{
    int no;
    char name[20];
    struct employee *next;
};

struct employee *insert(struct employee *head, struct employee *pb)
{
    struct employee *pn, *pt;
    pn = head;
    pt = NULL;
    while (pn)
    {
        if (pn->no == pb->no)
        {
            pb->next = pn;
            break;
        }
        pt = pn;
        pn = pn->next;
    }
    if (pt)
        pt->next = pb;
    else
        head = pb;
    return head;
}

main()
{
    struct employee a[6] =
    {
        101, "a101", NULL,
        103, "a103", NULL,
        102, "a102", NULL,
        101, "a101", NULL,
        103, "a103", NULL,
        102, "a102", NULL
    };
    struct employee *head = NULL;
    int i;
    for (i=0; i<6; ++i)
       head = insert(head, &a[i]);
    struct employee *h;
    for (h=head; h; h=h->next)
        printf("%d\t%s\n",h->no, h->name);
}
2018-04-16 15:33
chenhaiyuan
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2018-4-16
收藏
得分:0 
回复 2楼 吹水佬
谢谢,大佬。
2018-04-16 16:30
快速回复:链表节点插入
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044391 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved