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

来源:互联网 发布:淘宝店铺扣分 编辑:程序博客网 时间:2024/06/05 16:18
/*创建单链表,并将其打印出来。数据使用了随机数;*/#include <stdio.h>#include <stdlib.h>#include <time.h>#define N 16typedef 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;}

原创粉丝点击