函数Creatlink的功能是:创建带头节点的单项链表,并为各节点数据域赋0到m-1的值。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct aa
{
int data;
struct aa *next;
}NODE;
NODE *Creatlink(int n, int m)
{
NODE *h = NULL, *p, *s;
int i;
p = (NODE*)malloc(sizeof(NODE));
h = p;
p->next = NULL;
for (i = 1;i <= n;i++)
{
s = (NODE*)malloc(sizeof(NODE));
s->data = rand() % m;
s->next = NULL;
p->next = s;
p = s;
}
return h;
}
outlink(NODE *h)
{
NODE *p;
p = h->next;
printf("\n\nThe list :\n\n Head");
while (p)
{
printf("->%d", p->data);
p = p->next;
}
}
int main()
{
NODE *head;
head = Creatlink(8, 22);
outlink(head);
system("pause");
return 0;
}