#include<stdio.h>
#include<stdlib.h>
/
typedef struct Node
{
int value;
struct Node * next;
} NODE;
/单链表一次遍历逆序函数
NODE * reverse(NODE * head)
{
NODE * current = head->next;
NODE * p1 = NULL;
NODE * p2 = current->next;
if((head == NULL) || (head->next == NULL)) return NULL;
while(p2!=NULL)
{
current->next=p1;
p1=current;
current=p2;
p2=p2->next;
}
current->next = p1;
return current;
}
/测试。。
int main()
{
NODE * head = (NODE *)malloc(sizeof(NODE));
head->next=NULL;
NODE * p = head;
int i;
for(i=0;i<10;i++)
{
NODE * newnode = (NODE *)malloc(sizeof(NODE));
newnode->next=NULL;
newnode->value=i;
p->next = newnode;
p=p->next;
}
NODE * q = head->next;
printf("/nBefore reverse../n");
while(q!=NULL)
{
printf("%d->",q->value);
q=q->next;
}
q = reverse(head);
printf("/nAfter reverse../n");
while(q!=NULL)
{
printf("%d->",q->value);
q=q->next;
}
system("pause");
return 0;
}
06-21