标题:链表节点插入
只看楼主
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: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486103-1-1.html




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

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