C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄

/*
Filename: DoubleList.h
Description: Adout the doubly circular linked list algorithm
Date: November 8, 2012
*/

#ifndef DOUBLELIST_H
#define DOUBLELIST_H

#include <iostream>
using namespace std;

template<class T> class DoubleList;

/***************结点类定义***********************/
template<class T>
class DoubleNode
{
public:
	friend class DoubleList<T>;

public:
	DoubleNode()
	{
		prior = this;
		next = this;
	}

	DoubleNode(T val)
	{
		data = val;
		prior = this;
		next = this;
	}

	~DoubleNode()
	{

	}

public:
	void InsertAfter(T val);
	void InsertBefore(T val);

private:
	T data;
	DoubleNode<T> *prior;
	DoubleNode<T> *next;
};


/***************双向循环链表类定义***********************/
template<class T>
class DoubleList
{
public:
	DoubleList()
	{
		head = new DoubleNode<T>;
		head->next = head;
		head->prior = head;
	}

	~DoubleList()
	{
		Empty();
		head->next = NULL;
		head->prior = NULL;
		delete head;
	}

public:
	void AddToTail(int val); //后插
	void AddToHead(int val); //前插
	void Delete(int val); //删除结点
	void Empty(); //清空整个链表,仅余首结点
	void Print();

public:
	int Length() const;
	int IsEmpty();
	DoubleNode<T>* Find(T val);
	int IsIn(int val);

private:
	DoubleNode<T> *head;
};

#endif


/***************结点类实现***********************/
template<class T>
void DoubleNode<T>::InsertAfter(T val)
{
	DoubleNode<T> *newNode = new DoubleNode<T>(val);
	newNode->next = next;
	next->prior = newNode;
	newNode->prior = this;
	next = newNode;
}


template<class T>
void DoubleNode<T>::InsertBefore(T val)
{
	DoubleNode<T> *newNode = new DoubleNode<T>(val);
	newNode->prior = prior;
	prior->next = newNode;
	newNode->next = this;
	prior = newNode;
}


/***************双向循环链表类的实现***********************/
template<class T>
void DoubleList<T>::AddToTail(int val)
{
	DoubleNode<T> *newNode = new DoubleNode<T>(val);
	head->prior->next = newNode;
	newNode->prior = head->prior;

	newNode->next = head;
	head->prior = newNode;
}


template<class T>
void DoubleList<T>::AddToHead(int val)
{
	DoubleNode<T> *newNode = new DoubleNode<T>(val);
	newNode->next = head->next;
	head->next->prior = newNode;

	newNode->prior = head;
	head->next = newNode;

}

template<class T>
void DoubleList<T>::Delete(int val)
{
	DoubleNode<T> *delNode = Find(val);

	if (delNode == NULL)
	{
		return;
	}

	delNode->prior->next = delNode->next;
	delNode->next->prior = delNode->prior;
	delNode->next = NULL;
	delNode->prior = NULL;
	delete delNode;
}

template<class T>
void DoubleList<T>::Empty()
{
	DoubleNode<T>* tempNode = head->next;
	DoubleNode<T>* delNode = NULL;

	while (tempNode != head)
	{
		delNode = tempNode;
		tempNode->prior->next = tempNode->next;
		tempNode->next->prior = tempNode->prior;

		tempNode = tempNode->next;

		delNode->prior = NULL;
		delNode->next = NULL;
		delete delNode;
	}

}


template<class T>
void DoubleList<T>::Print()
{
	DoubleNode<T> *tempNode = head->next;
	cout << "共有" << Length() << "个结点" << endl;

	while (tempNode != head)
	{
		cout << tempNode->data << " ";
		tempNode = tempNode->next;
	}

	cout << endl;
}


template<class T>
int DoubleList<T>::Length() const
{
	int nLen = 0;
	DoubleNode<T> *tempNode = head->next;

	while (tempNode != head)
	{
		nLen++;
		tempNode = tempNode->next;
	}

	return nLen;
}


template<class T>
int DoubleList<T>::IsEmpty()
{
	if (head->next == head)
	{
		return 1;
	} 
	else
	{
		return 0;
	}
}


template<class T>
DoubleNode<T>* DoubleList<T>::Find(T val)
{
	DoubleNode<T> *tempNode = head->next;

	while (tempNode != head)
	{
		if (tempNode->data == val)
		{
			return tempNode;
		}

		tempNode = tempNode->next;
	}

	return NULL;
}


template<class T>
int DoubleList<T>::IsIn(int val)
{
	DoubleNode<T> *tempNode = head->next;

	while (tempNode != head)
	{
		if (tempNode->data == val)
		{
			return 1;
		}

		tempNode = tempNode->next;
	}

	return 0;
}


//测试
#include "DoubleList.h"


int main()
{
	DoubleList<double> dl;
	dl.AddToHead(20);
	dl.AddToHead(10);
	dl.AddToHead(30);
	dl.Print();
	cout << "-----------------------------" << endl;

	dl.AddToTail(11);
	dl.AddToTail(13);
	dl.AddToTail(15);
	dl.Print();
	cout << "-----------------------------" << endl;

	dl.Delete(11);
	dl.Print();
	cout << "-----------------------------" << endl;

	dl.Empty();
	dl.Print();
	
	system("pause");
	return 0;
}

如果有错误,请帮忙指出来,大家互相学习!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值