关于#链表#的问题:为什么报错“first”不是hnode成员(语言-c++)

为什么报错“first”不是hnode成员?
#include
#define elementype int
#define headatatype char
#define MAXSIZE 20
using namespace std;
typedef struct hnode {
    headatatype data;
    linklist* first;
}*headnode;
typedef struct lnode {       //链表的定义
    elementype data;
    lnode* next;

}*linklist;                     //定义链表linklist

bool initlist(linklist& l,headnode&e) {//链表初始化
    l->next = nullptr;
    e ->first = l;
    cout << "请输入链表信息" << endl;
    cin >>e->data;
    return true;
}

bool isempty(const headnode& e) {  //判断链表是否为空
    if (e->first == nullptr)
        return true;
    else return false;
}

bool destroylist(linklist& l,headnode&e) {    //销毁链表
    if (isempty(e )) {
        cout << "链表为空!" << endl;
        return false;
    }
    while (l) {
        auto t = l->next;
        delete[]l;
        l = t;
    }
    return true;
}


int getlength(const linklist& l, headnode& e) {//统计链表长度
    int a = 0;
    lnode *p = l;
    if (isempty(e)) {
        return 0;
    }
    while (p->next == nullptr)
    {
        p = p->next;
        a++;
    }
    return a;

}


bool getelem(const linklist& l,const int&a,elementype &b) {//获取第i个数据
    if (a < 0) {
        cout << "位置错误" << endl;
        return false;
    }
    lnode* p = l;
    for (int i = 1; i < a+1; i++) {
        p = p->next;
        if (p->next == nullptr) {//??
            return false;
        }
    }
    b = p->data;
    return true;
}
//按值查找链表(返回下标)
int locateelem(linklist& l, elementype& e) {
    lnode* p = l;
    int a = 1;
    while (p->data != e&&p->next!=nullptr){
        p = p->next;
        a++;
}
    if (p->next == nullptr) {
        cout << "链表中没有该元素!" << endl;
        return 0;
    }
    else return a;
}


//按值查找链表(返回指针)
lnode* locateelem_l(linklist& l, elementype& e)
{
    lnode* p;
    p = l->next;
    while (p && p->data != e) {
        p = p->next;
    }
    return p;




}

bool eraselist(linklist& l, const int& a) {   //删除元素
    if (a < 0) {
        cout << "删除位置错误" << endl;
        return false;
    }
    lnode* p = l;
    for (int i = 0; i < a - 1; i++) {
        l = l->next;
    }
    if (l->next == nullptr) {
        cout << "删除位置错误" << endl;
        return false;
    }
    for (int i = 0; i < a; i++) {
        p = p->next;
    }
    
    l->next = p->next;
    return true;
}

bool insertlist(linklist& l, const int& i, const elementype&e){//插入数据
    lnode* p = l;
    int j = 0;
    for (j = 0; j < i; j++) {
        p = p->next;

    }
    if (!p || i < 0) {
        cout << "错误" << endl;
        return false;
    }
    lnode* insert = new lnode; //linklist与linklist是否等价
    insert->data = e;
    insert->next = p->next;
    p->next = insert;
    return true;    
}

//头插法创建链表
void creatlisthead(linklist& l, const int a,headnode&e){
    elementype b;
    cout << "请输入数据" << endl;
    for (int j = 0; j < a; j++) {
        cin >> b;
        linklist p = new lnode;
        p->data = b;
        p->next = e->first;
        e->first = p;
    }
}




//尾插法创建链表(自己写的)
void creatlisttail(linklist& l, const int a, headnode& e) {
    cout << "请输入数据" << endl;
    lnode* r = l;
    int m;
    while (r->next != nullptr) {
        r = r->next;
        m++;
    }
    for (int i = 0; i < a; i++) {
        
        lnode* p = new lnode;
        cin >> p->data;
        if (i==0&&m==0){
            e->first = p;
            p->next = nullptr;
        
        }
        else {
            r->next = p;
            p->next = nullptr;
            r = p;
        }
    }
}

//答案(没有头节点)
void creatlisttail(linklist& l, const int n) {
    lnode* r = l;
    for (int i = 0; i < n; i++)
    {
        lnode* p = new lnode;
        cin >> p->data;
        p->next = r->next;
        r->next = p;
        r = r->next;
    }
}

int main() {
 headnode m;
 linklist n;
 bool t = initlist(n, m);
    if (t) {
        cout << "链表初始化成功!" << endl;
    }
    else cout << "链表初始化失败" << endl;


    int a;
    cout << "请输入链表数据个数" << endl;
    cin >> a;
    creatlisthead(n, a, m);




    return 0;
}



typedef struct hnode {
    headatatype data;
    linklist* first;
}*headnode;
这里还没有linklist的定义
嵌套定义了

以下代码可以编译(运行是否有问题另当别论)

#include<iostream>
#define elementype int
#define headatatype char
#define MAXSIZE 20
using namespace std;
typedef struct lnode {       //链表的定义
    elementype data;
    lnode* next;

}*linklist;  
typedef struct hnode {
    headatatype data;
    linklist first;
}*headnode;
                   //定义链表linklist

bool initlist(linklist& l,headnode&e) {//链表初始化
    l->next = nullptr;
    e ->first = l;
    cout << "请输入链表信息" << endl;
    cin >>e->data;
    return true;
}

bool isempty(const headnode& e) {  //判断链表是否为空
    if (e->first == nullptr)
        return true;
    else return false;
}

bool destroylist(linklist& l,headnode&e) {    //销毁链表
    if (isempty(e )) {
        cout << "链表为空!" << endl;
        return false;
    }
    while (l) {
        auto t = l->next;
        delete[]l;
        l = t;
    }
    return true;
}


int getlength(const linklist& l, headnode& e) {//统计链表长度
    int a = 0;
    lnode *p = l;
    if (isempty(e)) {
        return 0;
    }
    while (p->next == nullptr)
    {
        p = p->next;
        a++;
    }
    return a;

}


bool getelem(const linklist& l,const int&a,elementype &b) {//获取第i个数据
    if (a < 0) {
        cout << "位置错误" << endl;
        return false;
    }
    lnode* p = l;
    for (int i = 1; i < a+1; i++) {
        p = p->next;
        if (p->next == nullptr) {//??
            return false;
        }
    }
    b = p->data;
    return true;
}
//按值查找链表(返回下标)
int locateelem(linklist& l, elementype& e) {
    lnode* p = l;
    int a = 1;
    while (p->data != e&&p->next!=nullptr){
        p = p->next;
        a++;
}
    if (p->next == nullptr) {
        cout << "链表中没有该元素!" << endl;
        return 0;
    }
    else return a;
}


//按值查找链表(返回指针)
lnode* locateelem_l(linklist& l, elementype& e)
{
    lnode* p;
    p = l->next;
    while (p && p->data != e) {
        p = p->next;
    }
    return p;




}

bool eraselist(linklist& l, const int& a) {   //删除元素
    if (a < 0) {
        cout << "删除位置错误" << endl;
        return false;
    }
    lnode* p = l;
    for (int i = 0; i < a - 1; i++) {
        l = l->next;
    }
    if (l->next == nullptr) {
        cout << "删除位置错误" << endl;
        return false;
    }
    for (int i = 0; i < a; i++) {
        p = p->next;
    }
    
    l->next = p->next;
    return true;
}

bool insertlist(linklist& l, const int& i, const elementype&e){//插入数据
    lnode* p = l;
    int j = 0;
    for (j = 0; j < i; j++) {
        p = p->next;

    }
    if (!p || i < 0) {
        cout << "错误" << endl;
        return false;
    }
    lnode* insert = new lnode; //linklist与linklist是否等价
    insert->data = e;
    insert->next = p->next;
    p->next = insert;
    return true;    
}

//头插法创建链表
void creatlisthead(linklist& l, const int a,headnode&e){
    elementype b;
    cout << "请输入数据" << endl;
    for (int j = 0; j < a; j++) {
        cin >> b;
        linklist p = new lnode;
        p->data = b;
        p->next = e->first;
        e->first = p;
    }
}




//尾插法创建链表(自己写的)
void creatlisttail(linklist& l, const int a, headnode& e) {
    cout << "请输入数据" << endl;
    lnode* r = l;
    int m;
    while (r->next != nullptr) {
        r = r->next;
        m++;
    }
    for (int i = 0; i < a; i++) {
        
        lnode* p = new lnode;
        cin >> p->data;
        if (i==0&&m==0){
            e->first = p;
            p->next = nullptr;
        
        }
        else {
            r->next = p;
            p->next = nullptr;
            r = p;
        }
    }
}

//答案(没有头节点)
void creatlisttail(linklist& l, const int n) {
    lnode* r = l;
    for (int i = 0; i < n; i++)
    {
        lnode* p = new lnode;
        cin >> p->data;
        p->next = r->next;
        r->next = p;
        r = r->next;
    }
}

int main() {
 headnode m;
 linklist n;
 bool t = initlist(n, m);
    if (t) {
        cout << "链表初始化成功!" << endl;
    }
    else cout << "链表初始化失败" << endl;


    int a;
    cout << "请输入链表数据个数" << endl;
    cin >> a;
    creatlisthead(n, a, m);




    return 0;
}

typedef struct hnode {
    headatatype data;
    linklist* first;
}*headnode;
typedef struct lnode {       //链表的定义
    elementype data;
    lnode* next;
 
}*linklist;

这两个结构体定义交换一下顺序。

错误指向哪一行?

根据问题描述,我们可以推测出代码中可能存在以下问题:
1. 没有定义hnode结构体中的first成员变量。\n2. 在代码中使用了未定义的first成员变量。
针对这两种可能性,我们可以分别进行分析。
1. 没有定义hnode结构体中的first成员变量。
在链表的实现中,通常会定义一个头结点(head node),用于记录链表的头部信息,如链表的长度、第一个节点的位置等。因此,我们可以猜测hnode结构体中应该包含一个名为first的成员变量,用于记录链表的第一个节点的位置。
如果代码中没有定义hnode结构体中的first成员变量,那么在使用first时就会报错“不是hnode成员”。
解决方法:在hnode结构体中定义first成员变量。
2. 在代码中使用了未定义的first成员变量。
如果代码中已经定义了hnode结构体中的first成员变量,但在使用first时仍然报错“不是hnode成员”,那么可能是因为在代码中使用了未定义的first成员变量。
解决方法:检查代码中是否存在拼写错误或语法错误,确保使用的first成员变量与hnode结构体中定义的一致。
综上所述,要解决“报错‘first’不是hnode成员”的问题,需要检查代码中是否定义了hnode结构体中的first成员变量,并确保在使用first时没有出现拼写错误或语法错误。