zoukankan      html  css  js  c++  java
  • 结构体的构造函数

    #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
Copyright © 2011-2022 走看看