node* reverse1(node * head)
{
node*p,*q,*r;
p = head;
q = head->next;
head->next = NULL;
while(q){
r = q->next;
q->next = p;
p = q;
q = r;
}
head=p;
/* while(p)
{
printf("%d ",p->num);
p=p->next;
}*/
printf("\n");
return head;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int data;
struct _node *next;
} node;
node*
reverse1(node * head)
{
node*p,*q,*r;
if( head != NULL )
{
p = head;
q = head->next;
head->next = NULL;
while(q){
r = q->next;
q->next = p;
p = q;
q = r;
}
head=p;
}
return head;
}
int
main( void )
{
node *link = NULL;
node *tmp = NULL;
int i = 0;
for( i = 0; i < 5; i++ )
{
tmp = malloc( sizeof( node ) );
if( tmp == NULL )
return -1;
tmp->data = i;
tmp->next = link;
link = tmp;
}
tmp = link;
printf( "before reversal:\n" );
while( tmp )
{
printf( "%d ", tmp->data );
tmp = tmp->next;
}
putchar( '\n' );
link = reverse1( link );
printf( "after reversal:\n" );
tmp = link;
while( tmp )
{
printf( "%d ", tmp->data );
tmp = tmp->next;
}
putchar( '\n' );
return 0;
}
測了一下,應該可以使吧