标题:单链表问题
只看楼主
FORTHGOER
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2020-7-6
结帖率:0
 问题点数:0 回复次数:3 
单链表问题
程序代码:
//define struct type
typedef struct Lnode {

    int data;
    struct LNode* next;
    
}Lnode;

//creat Link list
void CreatList() {

    //creat Head pointer and Tail pointer
    Lnode** Head;
    Lnode* Tail;

    //creat an empyt Link list
    *Head = (Lnode* )malloc(sizeof(Lnode));
    (*Head)->next = NULL;
    
    Tail = *Head;

    //insert data 
    for (int i = 0; i < 3; ++i) {
        
        //creat new node
        Lnode* P = (Lnode* )malloc(sizeof(Lnode));
        
        //update date domain and pointer domain of new node
        P->data = i;
        P->next = NULL;

        //update pointer domain of before node
        Tail->next = P;

        //update Tail pointer Location
        Tail = P;

    }

}  




gcc 9.2.0 报错
.\Link.c: In function 'CreatList':
.\Link.c:45:20: warning: assignment to 'struct LNode *' from incompatible pointer type 'Lnode *' {aka 'struct Lnode *'} [-Wincompatible-pointer-types]
   45 |         Tail->next = P;
      |                    ^


我觉得没错啊 Tail->next 与 P 都是 Lnode* 类型啊
搜索更多相关主题的帖子: pointer Head Link struct next 
2021-03-11 15:00
FORTHGOER
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2020-7-6
得分:0 
回复 楼主 FORTHGOER
line5: struct LNode* next -> struct Lnode* next
2021-03-11 16:01
slbos
Rank: 2
等 级:论坛游民
威 望:2
帖 子:26
专家分:64
注 册:2021-3-20
得分:0 
额,Head是二级指针也是野指针吧,为什么可以把它指向的地址赋值呢?
*Head = (Lnode* )malloc(sizeof(Lnode));
2021-03-23 09:12
slbos
Rank: 2
等 级:论坛游民
威 望:2
帖 子:26
专家分:64
注 册:2021-3-20
得分:0 
//define struct type
typedef struct Lnode {

    int data;
    struct Lnode *next;
   
}Lnode;

typedef struct head{
    Lnode *Head;
    lnode *Tail;
}head;

//creat Link list
void CreatList() {

    //creat Head pointer and Tail pointer
    head h;
    h -> Head = NULL;
    h -> Tail = NULL;

    //insert data
    for (int i = 0; i < 3; ++i) {
        
        //creat new node
        Lnode* P = (Lnode* )malloc(sizeof(Lnode));
        
        //update date domain and pointer domain of new node
        P->data = i;
        P->next = NULL;

        //update pointer domain of before node
        h -> Tail->next = P;

        //update Tail pointer Location
        h -> Tail = P;

    }

}  
2021-04-10 11:46



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




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

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