C语言--单链表之调用输出函数
2014-11-17 17:27
211 查看
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
typedef struct chain
{
int data;
struct chain *next;
} list;
list *create()
{
int i ,n ;
list *head , *tail ,*p;
head = tail = NULL;
printf("Enter a number n : ");
scanf("%i",&n);
srand((unsigned)time(NULL));
for (i = 0 ;i <= n ;i++)
{
p = (list*)malloc(sizeof(list));
p->data = rand()%100 +1;
if ( head == NULL)
head = tail = p;
else
tail = tail->next;
tail->next = p;
}
return head;
}
void print_list(list *head)
{
list *p,*q;
p = head;
while(p)
{
printf("%4i",p->data);
q = p->next;
free(p);
p=q;
}
printf("\n");
}
int main (void)
{
list *head;
head=create();
print_list(head);
return 0;
}
测试:
[root@localhost Gcc]# ./alg1-4
Enter a number n : 10
7 61 81 81 14 69 91 88 8 22
[root@localhost Gcc]# ./alg1-4
Enter a number n : 6
90 33 77 83 53 72
# include <stdlib.h>
# include <time.h>
typedef struct chain
{
int data;
struct chain *next;
} list;
list *create()
{
int i ,n ;
list *head , *tail ,*p;
head = tail = NULL;
printf("Enter a number n : ");
scanf("%i",&n);
srand((unsigned)time(NULL));
for (i = 0 ;i <= n ;i++)
{
p = (list*)malloc(sizeof(list));
p->data = rand()%100 +1;
if ( head == NULL)
head = tail = p;
else
tail = tail->next;
tail->next = p;
}
return head;
}
void print_list(list *head)
{
list *p,*q;
p = head;
while(p)
{
printf("%4i",p->data);
q = p->next;
free(p);
p=q;
}
printf("\n");
}
int main (void)
{
list *head;
head=create();
print_list(head);
return 0;
}
测试:
[root@localhost Gcc]# ./alg1-4
Enter a number n : 10
7 61 81 81 14 69 91 88 8 22
[root@localhost Gcc]# ./alg1-4
Enter a number n : 6
90 33 77 83 53 72
相关文章推荐
- c语言:编写一个输出链表的函数print
- 第十一周—C语言 oj上机题目(输出空心三角形)(函数的调用)
- 编写函数,其功能是:将一个数字字符串转换为一个整数(不得调用C语言中提供的将字符串转换成整数的函数。)要求在主函数中输入该字符串并输出转换后的整数的值。
- c语言:编写一个输出链表的函数print
- 10月7日 c语言 函数调用 输入两个整数,要求输出其中值较大者,要求用函数找到最大数
- .net调用存储过程时的输出函数
- C语言中,时间调用处理的相关函数介绍
- c语言经典的排序函数swap():输入的两个整数按大小顺序输出。
- 关于显式调用DLL、隐式调用DLL和查看DLL输出函数
- C语言基本功教程系列 快速的函数调用
- C语言和C++语言的函数调用
- 关于C语言中函数调用和参数传递机制的探讨
- dll的输出函数使用__stdcall调用约定后,客户端用GetProcAddress出现的问题!
- C函数调用-不使用函数返回值,而用参数实现输入/输出的4种模式
- 关于显式调用DLL、隐式调用DLL和查看DLL输出函数
- 关于显式调用DLL、隐式调用DLL和查看DLL输出函数
- c语言里面的调用显示系统时间的函数及实现过程是什么
- C语言调用exe并将结果输出到文件(续)
- xsl:script与函数调用输出xsl:eval应用示例
- C语言中,链表的创建、输入、输出、链接。