单链表逆置的两种方法


代码及结果如下:

/*---------------------------------------------------------------------------------------------------------
链表逆置

方法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" );
}
结果:



  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值