单链表反转C语言实现

typedef struct node
{
	int num;
	struct node *next;
}Node,*pNode;

pNode CreatLinkList(int a)
{
	pNode p;
	pNode q;
	p = q = (pNode)malloc(sizeof(Node));
	p->num = a;
	while (a--)
	{
		

		p->next = (pNode)malloc(sizeof(Node));
		p = p->next;
		p->num = a;
	}
	p->next = NULL;
	return q;
}

void LinkListShow(pNode pHead)
{
//	assert(pHead != NULL);
	do 
	{
		printf("list_num = %d\n", pHead->num);
		pHead = pHead->next;
	}while (pHead != NULL);
}
pNode LinkListReversion(pNode pHead)
{
	pNode p, q, r;
	p = pHead;
	q = p->next;
	pHead->next = NULL;
	while (q != NULL)
	{
		r = q->next;
		q->next = p;
		p = q;
		q = r;
	}
	return p;
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	
	pNode pHead;
	pHead = CreatLinkList(3);
	LinkListShow(pHead);
	pHead = LinkListReversion(pHead);
	LinkListShow(pHead);
	argc = getchar();
	return 0;
}


你可能感兴趣的:(C语言)