首页 > 其他 > 详细

结构体的构造函数

时间:2018-09-17 23:43:22      阅读:213      评论:0      收藏:0      [点我收藏+]
#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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!