双方向チェーンテーブルのC++実装


転載して出典を明記するhttp://blog.csdn.net/hongkangwl/article/details/22286469
双向链表的C++实现_第1张图片 双向链表的C++实现_第2张图片 双向链表的C++实现_第3张图片 双向链表的C++实现_第4张图片
まずノードの構造を定義します
struct node 
{
	int date;
	node* prev;
	node* next;
};

いつものように、二重チェーンテーブルのクラスを構築します.
class doublelink
{
public:
	int doublelink_insert(doublelink* ptr,int position,int member);
	int doublelink_erase(doublelink* ptr, int position);
	void doublelink_display(doublelink* ptr, int num);
	int doublelink_getlength(doublelink* ptr);
	doublelink()
	{
		root = new node;
		root->prev = NULL;
		root->next = NULL;
		length = 0;

	}
protected:
private:
	int length;
	node* root;
};

要素の挿入
 
 
int doublelink::doublelink_insert(doublelink *ptr, int position, int member)
{
	node* nodeinsert = new node;
	nodeinsert->date = member;
	if (ptr->doublelink_getlength(ptr) == 0)
	{
		root->next = nodeinsert;
		nodeinsert->prev = nodeinsert;
		nodeinsert->next = nodeinsert;
		ptr->length++;
		return 0;
	} 
	else
	{
		if (position == 0)
		{
			/*	root->next->prev = nodeinsert;
			nodeinsert->prev = root->next->prev;
			nodeinsert->next = root->next;
			root->next->prev = nodeinsert;*/
			nodeinsert->prev = root->next->prev;
			nodeinsert->next = root->next;
			root->next->prev->next = nodeinsert;
			root->next->prev = nodeinsert;
			root->next = nodeinsert;
			ptr->length++;
			return 0;
		} 
		else
		{
			node* current =  root->next;
			for (int i = 0; i < position; i++)
			{
				current = current->next;
			}
			nodeinsert->next = current;
			nodeinsert->prev = current->prev;
			current->prev->next = nodeinsert;
			//nodeinsert->next = nodeinsert;	
			current->prev = nodeinsert;
			ptr->length++;
			return 0;
		}
	}
}

を します.0の に してください.
int doublelink::doublelink_erase(doublelink* ptr, int position)
{
	if (ptr->doublelink_getlength(ptr) == 0)
	{
		cout<<"    ,oop!!"<<endl;
		return 0;
	} 
	else
	{
		if (ptr->doublelink_getlength(ptr) == 1)
		{
			ptr->root->next = NULL;
			ptr->length--;
		} 
		else
		{
			node* deletenode = root->next;
			for (int i = 0 ; i < position; i++)
			{
				deletenode = deletenode->next;
			}
			deletenode->prev->next = deletenode->next;
			deletenode->next->prev = deletenode->prev;
			delete deletenode;
			ptr->length--;
		}
	}
}

な コードとテスト
#include <iostream>
using namespace std;
struct node 
{
	int date;
	node* prev;
	node* next;
};

class doublelink
{
public:
	int doublelink_insert(doublelink* ptr,int position,int member);
	int doublelink_erase(doublelink* ptr, int position);
	void doublelink_display(doublelink* ptr, int num);
	int doublelink_getlength(doublelink* ptr);
	doublelink()
	{
		root = new node;
		root->prev = NULL;
		root->next = NULL;
		length = 0;

	}
protected:
private:
	int length;
	node* root;
};
int doublelink::doublelink_erase(doublelink* ptr, int position)
{
	if (ptr->doublelink_getlength(ptr) == 0)
	{
		cout<<"    ,oop!!"<<endl;
		return 0;
	} 
	else
	{
		if (ptr->doublelink_getlength(ptr) == 1)
		{
			ptr->root->next = NULL;
			ptr->length--;
		} 
		else
		{
			node* deletenode = root->next;
			for (int i = 0 ; i < position; i++)
			{
				deletenode = deletenode->next;
			}
			deletenode->prev->next = deletenode->next;
			deletenode->next->prev = deletenode->prev;
			delete deletenode;
			ptr->length--;
		}
	}
}
void doublelink::doublelink_display(doublelink* ptr, int num)
{
	node* current = root->next;
	for (int i = 0; i < num; i++)
	{
		cout<<current->date<<" ";
		current = current->next;
	}
	cout<<endl;
}
int doublelink::doublelink_getlength(doublelink* ptr)
{
	return ptr->length;
}
int doublelink::doublelink_insert(doublelink *ptr, int position, int member)
{
	node* nodeinsert = new node;
	nodeinsert->date = member;
	if (ptr->doublelink_getlength(ptr) == 0)
	{
		root->next = nodeinsert;
		nodeinsert->prev = nodeinsert;
		nodeinsert->next = nodeinsert;
		ptr->length++;
		return 0;
	} 
	else
	{
		if (position == 0)
		{
			/*	root->next->prev = nodeinsert;
			nodeinsert->prev = root->next->prev;
			nodeinsert->next = root->next;
			root->next->prev = nodeinsert;*/
			nodeinsert->prev = root->next->prev;
			nodeinsert->next = root->next;
			root->next->prev->next = nodeinsert;
			root->next->prev = nodeinsert;
			root->next = nodeinsert;
			ptr->length++;
			return 0;
		} 
		else
		{
			node* current =  root->next;
			for (int i = 0; i < position; i++)
			{
				current = current->next;
			}
			nodeinsert->next = current;
			nodeinsert->prev = current->prev;
			current->prev->next = nodeinsert;
			//nodeinsert->next = nodeinsert;	
			current->prev = nodeinsert;
			ptr->length++;
			return 0;
		}
	}
}


int main()
{

	doublelink* doublelink_ptr = new doublelink;
	for (int i = 0; i < 10; i++)
	{
		doublelink_ptr->doublelink_insert(doublelink_ptr,0,i);
	}
	doublelink_ptr->doublelink_display(doublelink_ptr,20);
	doublelink_ptr->doublelink_erase(doublelink_ptr,2);
	doublelink_ptr->doublelink_display(doublelink_ptr,20);
	return 0;
}

     C++  _ 5
テストは しい~~