m0_74062574 2023-03-24 12:24 采纳率: 76.5%
浏览 44
已结题

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

为什么报错“first”不是hnode成员?
#include<iostream>
#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;
}



  • 写回答

5条回答 默认 最新

  • threenewbee 2023-03-24 12:37
    关注
    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 创建了问题 3月24日

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题