单链表的逆序打印

(1)递归思想

void PrintTailToHead(pList plist)//递归
{
	if (plist == NULL)
		return;
	else
		PrintTailToHead(plist->next);
	printf("%d ", plist->data);
}

(2)逐个遍历

void PrintTailToHead1(pList plist)
{
	pList cur = plist;
	pList p = NULL;
	pList q = NULL;
	while (cur != NULL)//遍历到最后一个节点
	{
		p = cur;
		cur = cur->next;
	}
	while (p != plist)//确定打印次数,当p==plist时,打印对应的数
	{
		cur = plist;
		while (cur != p)//打印那个数
		{
			q = cur;
			cur = cur->next;
		}
		printf("%d ", p->data);
		p = q;
	}
	printf("%d ", p->data);
}
l

猜你喜欢

转载自blog.csdn.net/oldwang1999/article/details/80794468