/*
創建單鏈表,並將其打印出來。數據使用了隨機數;
*/
#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;
}
C語言——單鏈表創建練習題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.