为什么报错“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;
}