c++デュアルチェーンテーブル【コンストラクション関数、演算子の再ロード、コンストラクション関数、削除・変更および逆置きなど】


c++における双方向チェーンテーブルの書き方は、主に実現される(増删查改、チェーンテーブル逆置、構造関数、演算子重荷重、など)
ヘッダファイルを作成するh
#pragma once

typedef int DataType;
class ListNode
{
	friend class List;//    
public:
	ListNode(const DataType x)
		:_data(x)
		, _prev(NULL)
		, _next(NULL)
	{}
private:
	DataType _data;
	ListNode* _prev;
	ListNode* _next;
};
class List
{
public:
	List()
		:_head(NULL)
		, _tail(NULL)
	{}
	//   
	List(const List& s)
		:_head(NULL)
		, _tail(NULL)
	{
		ListNode* tmp = s._head;
		while (tmp)
		{
			this->PushBack(tmp->_data);
			tmp = tmp->_next;
		}
	}
	//    
	List& operator=(List& s)
	{
		swap(_head, s._head);
		swap(_tail, s._tail);
		return *this;
	}
public:
	void Clear();
	void PrintList();
	void PushBack(DataType x);
	void PopBack();
	void PushFront(DataType x);
	void PopFront();
	void Insert(ListNode* pos, DataType x);
	void Erase(ListNode* pos);
	ListNode* Find(DataType x);
	//void Reverse();
	List Reverse();
private:
	ListNode* _head;
	ListNode* _tail;
};

各関数の実装
#include<iostream>
using namespace std;

#include"List.h"
#include<assert.h>

void List::Clear()//     
{
	ListNode* cur = _head;
	while (cur)
	{
		ListNode* del = cur;
		cur = cur->_next;
		delete del;
		del = NULL;
	}
}

void List::PrintList()//     
{
	ListNode* cur=_head;
	while (cur)
	{
		cout << cur->_data << "->";
		cur = cur->_next;
	}
	cout << "NULL" << endl;
}

void List::PushBack(DataType x)//  
{
	if (NULL == _head)
	{
		_head = new ListNode(x);
		_tail = _head;
	}
	else
	{
		ListNode* tmp = new ListNode(x); 
		_tail->_next = tmp;
		tmp->_prev = _tail;
		tmp->_next = NULL;
		_tail = tmp;
	}
}

void List::PopBack()//  
{
	if (NULL == _head)
	{
		cout << "List is empty!" << endl;
	}
	else if (_head == _tail)
	{
		delete _head;
		_head = _tail = NULL;
	}
	else
	{//                  
		ListNode* cur = _tail;
		_tail = cur->_prev;
		_tail->_next = NULL;
		delete cur;
		cur = NULL;
	}
}

void List::PushFront(DataType x)//  
{
	ListNode* tmp = _head;
	_head = new ListNode(x);
	_head->_prev = NULL;
	_head->_next = tmp;
}

void List::PopFront()//  
{
	if (NULL == _head)
	{
		cout << "SList is empty!" << endl;
	}
	else if (NULL == _head->_next)
	{
		delete _head;
		_head = NULL;
	}
	else
	{
		ListNode* tmp = _head->_next;
		delete _head;
		_head = tmp;
		tmp->_prev = NULL;
	}
}

ListNode* List::Find(DataType x)//  x
{
	ListNode* cur = _head;
	while (cur)
	{
		if (x == cur->_data)
			return cur;
		cur = cur->_next;
	}
	return NULL;
}

void List::Insert(ListNode* pos, DataType x)//       x
{
	assert(pos);
	if (NULL == pos->_next)
		List::PushBack(x);
	else if (_head == pos)
		List::PushFront(x);
	else
	{
		ListNode* cur = new ListNode(x);
		ListNode* prev = pos->_prev;
		prev->_next = cur;
		cur->_prev = prev;
		cur->_next = pos;
		pos->_prev = cur;
	}
}

void List::Erase(ListNode* pos)//    pos
{
	assert(pos);
	if (NULL == pos->_next)
		List::PopBack();
	else if (_head == pos)
		List::PopFront();
	else
	{
		ListNode* prev = pos->_prev;
		ListNode* next = pos->_next;
		next->_prev = prev;
		prev->_next = next;
	    delete pos;
		pos = NULL;
	}
}
//     
//      ,        ,       
//void List::Reverse()
//{
//	ListNode* begin = _head;
//	ListNode* end = _tail;
//	//                 ;                   
//	while (begin != end && begin->_prev != end)
//	{
//		swap(begin->_data, end->_data);
//		begin = begin->_next;
//		end = end->_prev;
//	}
//}

//      ,            
//void List::Reverse()
//{
//	swap(_head, _tail);
//	ListNode* cur = _head;
//	while (cur)
//	{
//		swap(cur->_prev,cur->_next);
//		cur = cur->_next;
//	}
//}

//     ,       
List List::Reverse()
{
	if (NULL == _head)
	{
		cout << "SList is empty!" << endl;
	}
	else if(NULL != _head->_next)
	{
		List NewList;
		ListNode* cur = _head->_next;
		ListNode* tmp = _head;//     ,      _next     
		while (cur)
		{
			this->PushFront(cur->_data);
			cur = cur->_next;
		}
		tmp->_next = NULL;
		return NewList;
	}
	return *this;
}