#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct NODE
{
int value;
struct NODE *next;
} Node, *LinkedList;
LinkedList create_linkedlist(int n)
{
LinkedList header;
LinkedList p;
int i = 0;
header = (LinkedList)malloc(sizeof(Node));
header->value = n;
printf("链表的长度是: %3d\n\n", header->value);
p = header;
p->next = NULL;
srand(time(0));
for(i = 0; i < n; i++)
{
LinkedList pn = (LinkedList)malloc(sizeof(Node));
if(!pn)
{
printf("唉,所谓人生...嗯。。。");
exit(-1);
}
pn->value = rand() % 100 + 1;
p->next = pn;
pn->next = NULL;
p = pn;
}
return header;
}
void print_list(LinkedList list)
{
LinkedList pt = list->next;
int count = 0;
while(pt != NULL)
{
count++;
printf("第%d个节点的值是:%3d\n",count, pt->value);
pt = pt->next;
}
printf("\n");
return;
}
int
create_sl_2(LinkedList *List, int size)
{
LinkedList p = NULL;
srand(time(0));
int i = 0;
*List = (LinkedList)malloc(sizeof(Node));
if(*List == NULL)
return -1;
(*List)->next = NULL;
for(i = 0; i < size; i++)
{
p = (LinkedList)malloc(sizeof(Node));
if(p == NULL)
return -1;
p->value = rand() % 100 + 1;
p->next = (*List)->next;
(*List)->next = p;
}
}
int main()
{
LinkedList list = NULL;
if(create_sl_2(&list, 5))
{
printf("创建成功...\n\n");
}else
{
printf("创建失败...\n\n");
}
print_list(list);
return 0;
}