#include<bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
ListNode(int x) :val(x), next(NULL) {};
};
ListNode* CreateListNode(int arr[], int n)
{
ListNode* head;
head = new ListNode(arr[0]);
ListNode* cur;
cur = head;
for (int i = 1; i < n; i++)
{
cur->next = new ListNode(arr[i]);
cur = cur->next;
}
return head;
}
int main()
{
int n;
scanf("%d", &n);
int i;
int a[100];
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
ListNode* head = CreateListNode(a, n);
while (head != NULL)
{
printf("%d ", head->val);
head = head->next;
}
}
结构体的第三行就是构造函数,可以进行new ListNode(a[i])这样的构造
原文:https://www.cnblogs.com/legendcong/p/9665478.html