sinat_40402252
sinat_40402252
2017-09-26 07:32

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

  • c++
  • 数据结构

#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条回答