legendcong

结构体的构造函数

2018-09-17 22:53  legend聪  阅读(4889)  评论(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])这样的构造

分类:

技术点:

相关文章:

  • 2021-11-01
  • 2021-09-30
  • 2021-07-16
  • 2018-01-18
  • 2021-09-10
  • 2021-06-11
  • 2021-11-05
猜你喜欢
  • 2021-11-19
  • 2021-11-23
  • 2021-11-23
  • 2021-10-16
  • 2021-05-18
  • 2021-06-30
  • 2021-08-03
相关资源
相似解决方案