单向链表的操作

以单向链表为例

创建链表

struct student {
	int age;
	int num;
	struct student *next;
};

typedef struct student LIST;

LIST *CreateList(void)
{
	LIST *h, *prev, *cur;
	int i, n;

	srand((int)time(0));
	h = NULL;
	for (i = 0; i < 8; i++) {
		cur = (LIST *)malloc(sizeof(LIST));
		cur->next = NULL;
		cur->age = rand() % 100;
		cur->num = i;
		
		if (h == NULL) {
			h = cur;
		} else {
			prev->next = cur;
		}
		prev = cur;
		printf("Node[%d] = %d\n",i ,cur->age);
	}
	return h;
}

打印链表

void Displist(LIST *h)
{
	int i;
	LIST *p;

	p = h;
	while (p) {
		printf("node[%d].age = %d\n", p->num, p->age);
		p = p->next;
	}
}
int main()
{
	LIST *head;
	printf("CreateList --------\n");
	head = CreateList();
	printf("Displist--------\n");
	Displist(head);
	printf("Displist-over-------\n");

}

 

 

songchong@srv-artek-pad:~/mytest/list$ gcc -o list-1 list-1.c 
songchong@srv-artek-pad:~/mytest/list$ ./list-1               
CreateList --------
Node[0] = 14
Node[1] = 35
Node[2] = 90
Node[3] = 61
Node[4] = 34
Node[5] = 60
Node[6] = 88
Node[7] = 65
Displist--------
node[0].age = 14
node[1].age = 35
node[2].age = 90
node[3].age = 61
node[4].age = 34
node[5].age = 60
node[6].age = 88
node[7].age = 65
Displist-over-------
songchong@srv-artek-pad:~/mytest/list$ 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值