6-5 头插法创建单链表(C) (25 分)-数据结构第2章

struct Node* buildLinkedList(int* arr, int n)
{

	struct Node* head = malloc(n * sizeof(struct Node));

	struct Node* p = head;
    int i;
	for (i = n-1; i >= 0; i--)
	{
		struct Node* pc= malloc(sizeof(struct Node));

		pc->data = arr[i];

		pc->link = NULL;

		p->link = pc;

		p = pc;

	}

	return head;


}    /* 头插法建立单链表 */;
void printLinkedList(struct Node* head)
{
	struct Node* p = head->link;

	int i = 0;
	while (p)
	{
		if (i++)printf(" ");

		printf("%d", p->data);
		p = p->link;
	}


}       /* 打印链表 */

 

你可能感兴趣的:(数据结构PTA,题解)