建立单链表后,(尾插法)主函数调用链表函数却没输出为什么?

#include
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;

}LNode;

void creatLinkListR(LNode &head)
{
head = (LNode
)malloc(sizeof(LNode));
head->next = NULL;
LNode p = NULL;
LNode *r = head;
cout << "Please input the length of LinkList you wanna built :" << endl;
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
p = (LNode
)malloc(sizeof(LNode));
p ->next= NULL;
cout << "Please intput p->data" << endl;
cin >> p->data;
p->next = r->next;
r->next = p->next;
r = p;
}
r->next = NULL;
}
int main()
{

LNode *head=(LNode*)malloc(sizeof(LNode));
head = NULL;
creatLinkListR(*&head);

LNode *p = head->next;

while (p != NULL)
{
    cout << p->data << " ";
    p = p->next;
}
cout << endl;
return 0;

}

1个回答

p->next = r->next;
r->next = p->next;

出问题了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问