m0_73883071 2022-10-26 20:00 采纳率: 100%
浏览 13
已结题

尾插法创建链表的错误

#include
#include <stdlib.h>

using namespace std;

typedef struct Node
{
int data;
struct Node* next;
}Node;

void initList(Node* hNode)
{
hNode = new Node;
hNode -> next = NULL;
}

void creatList(Node* hNode)
{
Node* tNode = hNode;

int Length;
cout << "请输入链表的长度:";
cin >> Length;

cout << "请输入链表的数据:";
for(int i = 0; i < Length; i++)
{
    Node* cNode = new Node;
    cNode -> next = NULL;
    cin >> cNode -> data;

    tNode -> next = cNode;
    tNode = cNode;
}

}

void printList(Node* hNode)
{
Node* p;
p = hNode -> next;
cout << "输出链表中的数据元素:" << endl;
while(p)
{
cout << p->data << " ";
p = p -> next;
}
}

int main(void)
{
Node* Mylist;

initList(Mylist);

creatList(Mylist);

printList(Mylist);

return 0;

}

img


这个错误有什么解决的方法

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-10-26 20:06
    关注

    初始化函数有问题,参数加个&符号,改为引用类型才行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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