#include<stdlib.h>
#include<stdio.h>
typedef struct node {
int data;
struct node *next;
}Node;
Node *CreatList( int n ) /*n用于指定链表长度*/
{
int val, i;
Node *phead, *p;
phead = ( Node * )malloc( sizeof( Node ) ); /*建立一个带头结点的链表*/
phead->next = NULL;
printf( "输入链表节点数据:\n" );
for( i = 0; i < n; i++ )
{
scanf( "%d", &val );
p = ( Node * )malloc( sizeof( Node ) );
p->data = val;
p->next = phead->next; /*倒插*/
phead->next = p;
}
return phead;
}
void ReverseList( Node *phead )
{
Node *p, *q, *r;
p = phead->next;
q = NULL;
r = NULL;
while( p->next != NULL ) /*r, q分别用于标记p的前后结点*/
{
q = p->next;
p->next = r;
r = p;
p = q;
}
p->next = r;
phead->next = p;
}
void PrintList( Node *phead )
{
Node *p;
p = phead->next;
while( p != NULL )
{
printf( "%d ", p->data );
p = p->next;
}
printf( "\n" );
}
main()
{
Node *phead;
phead = CreatList( 10 );
PrintList( phead );
ReverseList( phead );
PrintList( phead );
}