反转一个单链表

#include 
#include 
typedef int sdatatype;
typedef struct slistnode//单链表节点
{
	sdatatype _data;
	struct slistnode *pnext;
}node,*pnode;
typedef struct slist//给一个头指针保存第一个节点的地址
{
	pnode _phead;
}slist, *pslist;
void  reversallist(slist *s) {
	pnode p1 = s->_phead;
	pnode p2 = s->_phead->pnext;
	pnode p3 = p2->pnext;
	p1->pnext = NULL;//使头结点指向NULL
	while (p3->pnext!=NULL) {////三个指针依次后移,每次改变一个节点的pnext
		p2->pnext = p1;
		p1 = p2;
		p2 = p3;
		p3 = p3->pnext;
	}//最后几个节点指向
	p2->pnext = p1;
	p1 = p2;
	p2 = p3;
	p2->pnext = p1;
	s->_phead = p2;
}

 

你可能感兴趣的:(数据结构)