无需多解释,注意创建链表时对尾指针的处理,以及nullptr和new,malloc是C里面的。
#include <iostream>
using namespace std;
struct LinkNode
{
int value;
LinkNode* next;
};
LinkNode* CreatLinkNode(LinkNode* head)
{
int i=0;
int v=0;
LinkNode *p,*s;
cout << "Please input number1:" << endl;
while(cin>>v)
{
s = new LinkNode;
if (head==nullptr)
{
head = s;
}
else
{
p->next = s;
}
s->value = v;
p = s;
i++;
cout << "Please input number" <<i+1 <<":" <<endl;
}
p->next = nullptr;
return(head);
}
void PrintLinkNode(LinkNode* p)
{
if(p==nullptr)
{
cout << "No data" << endl;
}
else
{
cout << "The LinkNode:" << endl;
while(p != nullptr)
{
cout << p->value << endl;
p = p->next;
}
cout << "there is no more." << endl;
}
}
int main()
{
LinkNode* head = nullptr;
head = CreatLinkNode(head);
PrintLinkNode(head);
return 0;
}