此小程序仅产生用伪随机数初始化的单链表,没有定义对各链表进行操作的功能。 //伪随机数生成链表程序 #include<malloc.h> #include<stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> #define null 0 #define MAX 100 struct node //链表结点结构 { long Number; struct node *Next; }; struct node *Creat(int k) //创建结点函数Creat(),参数k为想创建的链表结点个数 { int count=0; //用来计算链表的结点个数 struct node *p1; struct node *p2; struct node *head=null; p1=p2=(struct node *)malloc(sizeof(struct node)); //开辟一段可用内存单元 p1->Number=p2->Number=rand()%100; do { if(count==0) //是否开辟的是第一个结点 { head=p2; } else { p1->Next=p2; p1=p2; p2=(struct node *)malloc(sizeof(struct node)); p2->Number=rand()%100; } count++; }while(count!=k+1); p1->Next=null; return(head); } void View(struct node *head) //查看链表内容函数View() { struct node *p; p=head; while(p->Next!=null) { printf("%ld ",p->Number); p=p->Next; } printf("%ld ",p->Number); printf("/n"); } int Number_of_ListNode(struct node *head) //统计下链表结点个数 { struct node *pt; pt=head; int count=0; while( pt->Next != null ) { ++count; pt=pt->Next; } return (count+1); } void main() { int n,k; printf("输入要生成的单链表个数:/n"); scanf("%d",&n); printf("输入每个单链表的长度(大小):/n"); scanf("%d",&k); struct node *head[MAX]; srand((unsigned)time(NULL)); for(int i=1;i<=n;i++) { head[i]=Creat(k); printf("链表%d里面的内容如下:/n",i); View(head[i]); printf("链表结点个数:%d/n",Number_of_ListNode(head[i])); printf("/n"); } getch(); } 运行结果如下: