西南交通大学数据结构考研真题:链表总结

2005_3:

单链表排序:

创建单链表

//创建链表
Employee* Create( int a[], int len)
{
	Employee* L;
	//创建头结点
	L = (Employee*)malloc(sizeof(Employee));
	L->pNext = NULL;

	Employee *prior, *pcur;
	prior = L;
	for (int i =0; i < len; i++)
	{
		pcur = (Employee*)malloc(sizeof(Employee));
		pcur->eID = a[i];		
		pcur->pNext = prior->pNext;
		prior->pNext = pcur;
		prior = pcur;
	}
	return L;
}

打印单链表:

//打印链表
void Print(Employee* L)
{
	if (L==NULL)
	{
		return;
	}

	Employee* p;
	p = L->pNext;
	while (p != NULL)
	{
		printf("%d,\t", p->eID);
		p = p->pNext;
	}
	//退出循环时,p==L
	printf("\n");
}

测试demo:

int _tmain(int argc, _TCHAR* argv[])
{
	int a[] = { 3,2,1,9,7,6,5 };
	Employee* La=Create(a, 7);
	printf("*********La:**********\n");
	Print(La);
	Employee* Lb=elDsequence(La);
	printf("*********Lb:**********\n");
	Print(Lb);
	printf("*********La1:**********\n");
	Print(La);
	int a3=0;
	return 0;
}

测试结果:

 

你可能感兴趣的:(考研真题)