用递归实现链表的创建

来源:互联网 发布:博思数据 igbt销量 编辑:程序博客网 时间:2024/05/18 01:40

一.问题描述
应用递归创建链表,只是为了练习使用递归,所以比较消耗时间,程序运行起来比较慢。
二.源代码
#include<stdio.h>#include <stdlib.h>typedef struct date{int a;struct date *next;}Date;//将结构体重命名为Datevoid Creat(Date *m) //函数参数为链表的结点{Date *n;n = (Date *)malloc(sizeof(Date));scanf("%d", &n->a);if(n->a){m->next = n;m = n;Creat(m);}else{m->next = NULL;free(n);}}void main()  {  Date *head,*p;//p是为了将结点链接起来head = p = (Date *)malloc(sizeof(Date));printf("请输入一个整数(输入‘0’结束):\n");Creat(p);head = head->next;if(head == NULL)printf("链表为空!\n");else{while(head){printf("%3d", head->a);head = head->next;}printf("\n");}}


0 0
原创粉丝点击