您的位置:首页 > 编程语言 > C语言/C++

C语言——单链表创建练习题

2013-03-13 15:20 344 查看
/*
创建单链表,并将其打印出来。数据使用了随机数;

*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 16
typedef struct node *link;
struct node {
int item;
link next;
};
link NODE(int item, link next)
{
link t = malloc(sizeof *t);
t->item = item;
t->next = next;
return t;
}
void show_list(link head)
{
link t;
for (t=head; t; t=t->next) printf("%3d", t->item);
printf("\n");
}
link insert_node(link head, int item)
{
link x, y;
for (y=head, x=y; y; x=y, y=y->next)
if (item <= y->item) break;
if (x==y) head = NODE(item, head);
else x->next = NODE(item, y);
return head;
}
int main()
{
int i;
link head = NULL;
srand(time(NULL));
for (i=0; i<N; i++) head = insert_node(head, rand()%100);
show_list(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: