代码及结果如下:
/*---------------------------------------------------------------------------------------------------------
链表逆置
方法1:修改节点指针(本地逆置)
方法2:单链表在逻辑上等价于一个数组,对链表中每个节点(头节点除外)的值域进行逆置,即可得到链表的逆置
----------------------------------------------------------------------------------------------------------*/
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
typedef int ElemType;
#define LOCALREVERSE 0
#define NONLOCALREVERSE 1
struct Node
{
ElemType value;
Node *next;
};
int ListInit( Node **head ); // 链表初始化
int ListAppend( Node **head, const ElemType &val ); // 在链表末尾添加一个节点,节点的值为value
void ListReverse( Node **head ); // 单链表逆置
void ListTraverse( Node **head ); // 遍历链表
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
Node *head = NULL;
ListInit( &head );
for( i=0; i<9; i++ )
ListAppend( &head, i );
ListTraverse( &head );
ListReverse( &head );
ListTraverse( &head );
system( "pause" );
return 0;
}
/**************************************************************************************
* Author: Sky
* Functiuon: ListInit
* Description: 初始化一个空链表(只含一个头结点)
* Access Level: N
* Input: head 指向头节点的指针
* Output: N
* Return: 0/-1 成功/失败
**************************************************************************************/
int ListInit( Node **head )
{
*head = (Node *)malloc( sizeof( Node ) );
if( NULL == head )
return -1;
else
{
(*head)->next = NULL;
}
return 0;
}
/**************************************************************************************
* Author: Sky
* Functiuon: ListAppend
* Description: 在链表末尾添加一个节点,节点的值域为传入的形参val
* Access Level: N
* Input: head 指向头节点的指针
* Output: N
* Return: 0/-1 成功/失败
**************************************************************************************/
int ListAppend( Node **head, const ElemType &val )
{
Node *New = NULL;
Node *Cur = *head;
if( NULL == *head )
{
printf( "the list hasn't be init\n" );
return -1;
}
else
{
New = (Node *)malloc( sizeof(Node) );
if( NULL == New )
{
printf("malloc failed!\n");
return -1;
}
else
{
New->value = val;
New->next = NULL;
while( NULL != Cur->next )
{
Cur = Cur->next;
}
Cur->next = New;
}
}
return 0;
}
/**************************************************************************************
* Author: Sky
* Functiuon: ListReverse
* Description: 对一个链表实现逆置(原地逆置)
* Access Level: N
* Input: head 指向头节点的指针
* Output: N
* Return: N
**************************************************************************************/
#if LOCALREVERSE
void ListReverse( Node **head )
{
Node *Cur = NULL, *Pre = NULL, *Next = NULL;
Pre = *head;
Cur = (*head)->next;
Next = Cur->next;
// 反转第一个节点
Cur->next = NULL;
Pre = Cur;
Cur = Next;
while( NULL != Cur )
{
Next = Cur->next;
Cur->next = Pre;
Pre = Cur;
Cur = Next;
}
(*head)->next = Pre;
}
#elif NONLOCALREVERSE
// Q:如何逆置单链表的数据域?
// 思路:定义一个指针数组,大小为链表大小,元素为指向链表节点的指针
void ListReverse( Node **head )
{
Node *Cur = (*head)->next;
int Len = 0, i = 0;
// 1、计算链表长度
while( NULL != Cur )
{
Len++;
Cur = Cur->next;
}
// 2、动态声明指针数组,赋值
Node **a = new Node *[Len];
Cur = (*head)->next;
while( NULL != Cur )
{
a[i++] = Cur;
Cur = Cur->next;
}
// 3、逆置
int low = 0, high = Len-1;
ElemType tmp;
while( low <high )
{
tmp = a[low]->value;
a[low]->value = a[high]->value;
a[high]->value = tmp;
low++;
high--;
}
}
#endif
/**************************************************************************************
* Author: Sky
* Functiuon: ListInit
* Description: 遍历链表
* Access Level: N
* Input: head 指向头节点的指针
* Output: N
* Return: N
**************************************************************************************/
void ListTraverse( Node **head )
{
Node *Cur = (*head)->next;
if( NULL == *head )
return ;
while( NULL != Cur )
{
printf( "%d ", Cur->value );
Cur = Cur->next;
}
printf( "\n" );
}
结果: