初学数据结构,自己写单向链表时遇到如下问题
typedef struct lNode* list;
struct lNode
{
int val;
list next;
};
//尾插法插入节点
void addList(list& l, int val)
{
list lastCell = new lNode;
lastCell->val = val;
list temp = l; //构建临时节点指向表头
if (!l) //是否为第一个插入点
{
l = lastCell;
return;
}
while (l->next) //不为空则找到链表最后一个节点
{
l = l->next;
}
l->next = lastCell;
l = temp; //遍历后重新使l指向表头
}
int main()
{
list l = new lNode;
l = NULL; //这里置空是为了方便判断第一个插入点
addList(l, 1);
addList(l, 2);
printList(l); //打印链表
return 0;
}
然后报错访问权限问题
发现是这类通常由链表初始化问题引起,参考以下文章: