码迷,mamicode.com
首页 > 编程语言 > 详细

C++算法之 倒序输出一个链表

时间:2014-12-12 16:48:03      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   sp   for   bs   代码   ad   as   

题目:给定一个头结点,倒叙输出一个链表

 

解法1:先将链表反转,在遍历输出

解法2:不修改链表自身的结构,动态申请一段空间,申请一个指针数组,数组内存放的指针指向链表的每个值,再遍历数组输出:

void PrintListBack(ListNode* head)
{
	int n = GetLength(head);
	ListNode** p = new ListNode*[n+1];
	p[n] = NULL;
	int i = 0;
	ListNode* pNode = head;
	while (i < n)
	{
		p[i] = pNode;
		pNode = pNode->m_pNext;
		++i;	
	}

	for (i = n-1; i>= 0; i--)
	{
		cout<<p[i]->m_nValue<<" ";
	}
}


解法3:利用栈,后进先出;

void PrintListBack2(ListNode* head)
{
	stack<ListNode> listStack;
	ListNode* pNode = head;
	while (pNode!=NULL)
	{
		listStack.push(pNode->m_nValue);
		pNode = pNode->m_pNext;
	}
	int i = listStack.size();
	for (;i>0;i--)
	{
		ListNode p = listStack.top();
		cout<<p.m_nValue<<endl;
		listStack.pop();
	}
}
void PrintListBack3(ListNode* head)
{
	stack<ListNode*> listStack;
	ListNode* pNode = head;
	while (pNode!=NULL)
	{
		listStack.push(pNode);
		pNode = pNode->m_pNext;
	}
	int i = listStack.size();
	for (;i>0;i--)
	{
		ListNode* p = listStack.top();
		cout<<p->m_nValue<<endl;
		listStack.pop();
	}
}


解法4:利用递归

void PrintListBack4(ListNode* head)
{
	if (head != NULL)
	{
		if (head->m_pNext != NULL)
		{
			PrintListBack4(head->m_pNext);
		}

		cout<<head->m_nValue<<endl;
	}
}


完整代码:

// PrintListBack.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;

struct ListNode 
{
	int        m_nValue;
	ListNode*  m_pNext;
	ListNode(int a):m_nValue(a),m_pNext(NULL)
	{

	}
};

int GetLength(ListNode* head)
{
	ListNode* pNode = head;
	int nCount = 0;

	while (pNode!= NULL)
	{
		nCount++;
		pNode = pNode->m_pNext;
	}


	return nCount;
}

void PrintListBack(ListNode* head)
{
	int n = GetLength(head);
	ListNode** p = new ListNode*[n+1];
	p[n] = NULL;
	int i = 0;
	ListNode* pNode = head;
	while (i < n)
	{
		p[i] = pNode;
		pNode = pNode->m_pNext;
		++i;	
	}

	for (i = n-1; i>= 0; i--)
	{
		cout<<p[i]->m_nValue<<" ";
	}
}


void PrintListBack2(ListNode* head)
{
	stack<ListNode> listStack;
	ListNode* pNode = head;
	while (pNode!=NULL)
	{
		listStack.push(pNode->m_nValue);
		pNode = pNode->m_pNext;
	}
	int i = listStack.size();
	for (;i>0;i--)
	{
		ListNode p = listStack.top();
		cout<<p.m_nValue<<endl;
		listStack.pop();
	}
}
void PrintListBack3(ListNode* head)
{
	stack<ListNode*> listStack;
	ListNode* pNode = head;
	while (pNode!=NULL)
	{
		listStack.push(pNode);
		pNode = pNode->m_pNext;
	}
	int i = listStack.size();
	for (;i>0;i--)
	{
		ListNode* p = listStack.top();
		cout<<p->m_nValue<<endl;
		listStack.pop();
	}
}

void PrintListBack4(ListNode* head)
{
	if (head != NULL)
	{
		if (head->m_pNext != NULL)
		{
			PrintListBack4(head->m_pNext);
		}

		cout<<head->m_nValue<<endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{

	ListNode*  head  = new ListNode(0);
	ListNode*  node1 = new ListNode(1);
	ListNode*  node2 = new ListNode(2);
	ListNode*  node3 = new ListNode(3);
	ListNode*  node4 = new ListNode(4);
	ListNode*  node5 = new ListNode(5);

	head->m_pNext  = node1;
	node1->m_pNext = node2;
	node2->m_pNext = node3;
	node3->m_pNext = node4;
	node4->m_pNext = node5;
	node5->m_pNext = NULL;

	int n = GetLength(head);
	cout<<"个数为:"<<n<<endl;

	PrintListBack4(head);



	getchar();
	return 0;
}


 

 

 

C++算法之 倒序输出一个链表

标签:io   ar   os   sp   for   bs   代码   ad   as   

原文地址:http://blog.csdn.net/djb100316878/article/details/41894335

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!