码迷,mamicode.com
首页 > 其他好文 > 详细

单链表的逆转

时间:2014-10-13 09:31:11      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:算法导论   单链表   逆转   

算法导论:10.2-7 给出一个 O(n) 时间的非递归过程,实现对一个含有 n 个元素的单链表的逆转。要求除存储链表本身所需的空间外,该过程只能使用固定大小的存储空间。

#ifndef _SINGLY_LINKED_LIST_H
#define _SINGLY_LINKED_LIST_H

/***********************************************************
10.2-7 单链表类,并实现了在 O(n) 时间内链表的逆转
************************************************************/

template <class T>
class SinglyLinkedList
{
public:
	// 一个表示链表结点的数据结构
	class Node{
	public:
		// 只有 StackUseSinglyLinkedList 才可以构造这个类型的对象
		// 访问这个类型的私有成员
		friend class SinglyLinkedList < T > ;
		// 结点的值
		T value;
	private:
		// 默认构造函数,要求类型 T 也要有默认构造函数
		Node() :_next(nullptr){}
		// 将构造函数设置为私有的,防止在外部创建这个类型的对象
		Node(const T& e) :_next(nullptr), value(e){}
		// 指向下一个元素,如果这个值为空,
		// 表示本结点是链表中的最后一个元素
		Node* _next;
	};

	SinglyLinkedList();
	~SinglyLinkedList();

	// 测试链表是否为空,空链表的头结点的 _next 为空
	bool empty() const { return _head->_next == nullptr; }

	void insert(const T&);

	void remove(const T&);

	Node* search(const T&) const;

	// 将链表内的结点逆转,并返回当前链表
	SinglyLinkedList* reverse();

	void print() const;
private:
	Node* _head;
};

template <class T>
SinglyLinkedList<T>::SinglyLinkedList()
{
	// 为头结点分配空间
	_head = new Node();
}

template <class T>
typename SinglyLinkedList<T>::Node* SinglyLinkedList<T>::search(const T& element) const{
	// 从链表头开始搜索再回到链表头
	auto node = _head->_next;
	while (node){
		if (node->value == element) return node;
		node = node->_next;
	}
	return nullptr;
}

template <class T>
void SinglyLinkedList<T>::insert(const T& element){
	// 在链表的头部插入元素,新元素指向头结点的下一个元素,头结节指向新元素
	// 为新元素分配空间,当从元素从链表中删除时要释放这个元素占用的空间
	Node* node = new Node(element);
	node->_next = _head->_next;
	_head->_next = node;
}

template <class T>
void SinglyLinkedList<T>::remove(const T& element){
	// 必找到目标元素的前驱结点
	auto node = _head->_next;
	auto prevNode = _head;
	while (node)
	{
		if (node->value == element){
			prevNode->_next = node->_next;
			delete node;
			break;
		}
		prevNode = node;
		node = node->_next;
	}
}

template <class T>
SinglyLinkedList<T>* SinglyLinkedList<T>::reverse(){
	auto node = _head->_next;
	// 记录下第一原第一个结点
	auto firstNode = node;
	while (node){
		auto nextNode = node->_next;

		// 将 node 结点插入到第一个结点之前
		node->_next = _head->_next;
		_head->_next = node;

		node = nextNode;
	}
	// 这时原第一个结点成为最后一个结点,应将它的 _next 置为 null,以标识链尾
	if (firstNode) firstNode->_next = nullptr;
	return this;
}

template <class T>
void SinglyLinkedList<T>::print() const{
	auto node = _head->_next;
	while (node){
		std::cout << node->value << " ";
		node = node->_next;
	}
	std::cout << std::endl;
}

template <class T>
SinglyLinkedList<T>::~SinglyLinkedList(){
	// 将链表中元素占用的空间释放
	Node* node = _head->_next;
	while (node)
	{
		Node *n = node->_next;
		delete node;
		node = n;
	}
	// 释放头结点占用的空间
	delete _head;
};


#endif


单链表的逆转

标签:算法导论   单链表   逆转   

原文地址:http://blog.csdn.net/nrj/article/details/40039467

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