1.代码
#include<iostream>
using namespace std
;
struct ListNode
{
int val
;
ListNode
* next
;
ListNode() :val(0), next(nullptr) {};
ListNode(int x
) : val(x
), next(nullptr) {};
};
ListNode
* create(int n
)
{
auto dummy
= new ListNode(0);
auto pre
= dummy
;
for (int i
= 0; i
< n
; i
++)
{
auto p
= new ListNode(0);
cin
>> p
->val
;
pre
->next
= p
;
pre
= pre
->next
;
}
return dummy
->next
;
}
void print(ListNode
* head
)
{
while (head
)
{
if (head
->next
== NULL)
cout
<< head
->val
<<"-> NULL";
else
cout
<< head
->val
<< "-> ";
head
= head
->next
;
}
cout
<< endl
;
}
int main()
{
int n
;
cout
<< "链表的长度为:" << endl
;
cin
>> n
;
cout
<< endl
;
cout
<< "依次输入元素的值(enter键换行)" << endl
;
auto head
= create(n
);
cout
<< "链表输出为:" << endl
;
print(head
);
system("pause");
return 0;
}
2.运行结果
有一说一,这代码属实垃圾,但是我又没想到更好的,还是太菜
转载请注明原文地址:https://ipadbbs.8miu.com/read-62630.html