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

【数据结构】双循环链表(c++)

时间:2015-05-28 21:32:23      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:数据结构   c++   双循环链表   

头文件:


#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

template<class Type>
class List;

// 结点类
template<class Type>
class NodeList
{
	friend class List<Type>;
public:
	NodeList();
	NodeList(Type d, NodeList<Type> *n = NULL, NodeList<Type> *m = NULL);
private:
	Type data;
	NodeList<Type>* next;
	NodeList<Type>* prio;
};

// 链表类
template<class Type>
class List
{
public:
	List();
	~List();
public:
	void show_list();
	void tail_insert(Type const &x);
	void head_insert(Type const &x);
	void sort();
	void head_delete();
	void tail_delete();
	void val_insert(Type const &x);
	NodeList<Type>* find(Type const &x);
	void val_delete(Type const &x);
	Type length();
	void reverse();
	void clear();
	void destroy();
	void quit_system(Type &x);
protected:
	NodeList<Type>* BuyNode(Type x = Type())
	{
		NodeList<Type>* p = new NodeList<Type>(x);
		assert(p != NULL);
		return p;
	}
private:
	NodeList<Type>* first;
	NodeList<Type>* last;
};

// 结点类的构造函数
template<class Type>
NodeList<Type>::NodeList()
{
	data = Type();
	next = NULL;
	prio = NULL;
}

template<class Type>
NodeList<Type>::NodeList(Type d, NodeList<Type> *n = NULL, NodeList<Type> *m = NULL)
{
	data = d;
	next = n;
	prio = m;
}

// 链表类的构造函数
template<class Type>
List<Type>::List()
{
	first = last = BuyNode();
	first->prio = last;
	last->next = first;
}

// 链表类的析构函数
template<class Type>
List<Type>::~List()
{
	destroy();
}

// 显示
template<class Type>
void List<Type>::show_list()
{
	NodeList<Type> *p = first->next;
	while (p != first)
	{
		cout << p->data << "->";
		p = p->next;
	}
	cout << "NULL" << endl;
}

// 尾插
template<class Type>
void List<Type>::tail_insert(Type const& x)
{
	NodeList<Type> *p = BuyNode(x);
	last->next = p;
	p->prio = last;
	p->next = first;
	first->prio = p;
	last = p;
	first->data++;
}

// 头插
template<class Type>
void List<Type>::head_insert(Type const &x)
{
	NodeList<Type> *p = BuyNode(x);
	if (first->data == 0)
	{
		tail_insert(x);
	}
	else
	{
		p->next = first->next;
		first->next = p;
		p->prio = first;
		p->next->prio = p;
		first->data++;
	}
}

// 排序
template<class Type>
void List<Type>::sort()
{
	if (first->data == 0)
	{
		return;
	}
	if (first->data == 1)
	{
		show_list();
	}
	NodeList<Type> *t = first->next;
	NodeList<Type> *p = t->next;
	NodeList<Type> *q = NULL;
	t->next = first;
	first->prio = t;
	p->prio = NULL;
	last = t;
	first->data = 1;
	while (p != first)
	{
		val_insert(p->data);
		q = p;
		p = p->next;
		delete q;
	}
}

// 头删
template<class Type>
void List<Type>::head_delete()
{
	if (first->data == 0)
	{
		cout << "the list is empty,cannot delete!" << endl;
		return;
	}
	NodeList<Type> *p = first->next;
	if (first->data == 1)
	{
		last = first;
		first->next = last;
		first->prio = last;
	}
	else
	{
		first->next = p->next;
		p->next->prio = first;
	}
	delete p;
	first->data--;
}

// 尾删
template<class Type>
void List<Type>::tail_delete()
{
	if (first->data == 0)
	{
		cout << "the list is empty,cannot delete!" << endl;
		return;
	}
	NodeList<Type> *s = last;
	last = last->prio;
	last->next = first;
	first->prio = last;
	delete s;
	first->data--;
}

// 按值插入
template<class Type>
void List<Type>::val_insert(Type const &x)
{
	if (first->data == 0)
	{
		tail_insert(x);
		return;
	}
	NodeList<Type> *p = first->next;
	NodeList<Type> *s = BuyNode(x);
	while (p->data < x && p->next != first)
	{
		p = p->next;
	}
	if (p->data >= x)
	{
		p->prio->next = s;
		s->prio = p->prio;
		s->next = p;
		p->prio = s;
		first->data++;
	}
	else
	{
		tail_insert(x);
	}
}

// 查找
template<class Type>
NodeList<Type>* List<Type>::find(Type const &x)
{
	NodeList<Type> *s = first->next;
	while (s != first)
	{
		if (s->data == x)
		{
			return s;
		}
		s = s->next;
	}
	return NULL;
}

// 按值删除
template<class Type>
void List<Type>::val_delete(Type const &x)
{
	if (first->data == 0)
	{
		cout << "the list is empty,cannot delete!" << endl;
		return;
	}
	NodeList<Type> *s = find(x);
	if (s == NULL)
	{
		cout << "the number is not exist,can not delete!" << endl;
		return;
	}
	if (s->next != first)
	{
		s->prio->next = s->next;
		s->next->prio = s->prio;
		first->data--;
		delete s;
	}
	else
	{
		tail_delete();
	}
}

// 求长度
template<class Type>
Type List<Type>::length()
{
	return first->data;
}

// 反转
template<class Type>
void List<Type>::reverse()
{
	if (first->data == 0)
	{
		return;
	}
	if (first->data == 1)
	{
		show_list();
	}
	NodeList<Type> *t = first->next;
	NodeList<Type> *p = t->next;
	NodeList<Type> *q = NULL;
	t->next = first;
	first->prio = t;
	p->prio = NULL;
	last = t;
	first->data = 1;
	while (p != first)
	{
		head_insert(p->data);
		q = p;
		p = p->next;
		delete q;
	}
}

// 清空
template<class Type>
void List<Type>::clear()
{
	if (first->data == 0)
	{
		return;
	}
	while (first->next != first)
	{
		tail_delete();
	}
}

// 摧毁
template<class Type>
void List<Type>::destroy()
{
	clear();
	delete first;
}

// 退出系统
template<class Type>
void List<Type>::quit_system(Type &x)
{
	x = 0;
}



主函数:


// 用c++实现单链表

#include "DCList.h"

int main()
{
	List<int> mylist;
	int input = 1;
	int insert;
	while (input)
	{
		cout << "*********************************************************************" << endl;
		cout << "*     [1] show_list                       [2] tail_insert           *" << endl;
		cout << "*     [3] head_insert                     [4] sort                  *" << endl;
		cout << "*     [5] head_delete                     [6] tail_delete           *" << endl;
		cout << "*     [7] val_insert           [8] find                  *" << endl;
		cout << "*     [9] val_delete                      [10] length               *" << endl;
		cout << "*     [11] reverse                        [12] clear                *" << endl;
		cout << "*     [13] destroy                        [14] quit_system          *" << endl;
		cout << "*********************************************************************" << endl;
		cout << "please choose:";
		cin >> input;
		switch (input)
		{
		case 1:
			mylist.show_list();
			break;
		case 2:
			cout << "please enter the number want to insert(-1 end):";
			while (cin >> insert, insert != -1)
			{
				mylist.tail_insert(insert);
			}
			break;
		case 3:
			cout << "please enter the number want to insert(-1 end):";
			while (cin >> insert, insert != -1)
			{
				mylist.head_insert(insert);
			}
			break;
		case 4:
			mylist.sort();
			break;
		case 5:
			mylist.head_delete();
			break;
		case 6:
			mylist.tail_delete();
			break;
		case 7:
			cout << "please enter the number want to insert:";
			cin >> insert;
			mylist.val_insert(insert);
			break;
		case 8:
			cout << "please enter the number want to find:";
			cin >> insert;
			if (mylist.find(insert) == NULL)
			{
				cout << "the number is not exist!" << endl;
			}
			else
			{
				cout << "the number at the " << mylist.find(insert) << endl;
			}
			break;
		case 9:
			cout << "please enter the number want to delete:";
			cin >> insert;
			mylist.val_delete(insert);
			break;
		case 10:
			cout << "the length is" << "  " << mylist.length() << endl;
			break;
		case 11:
			mylist.reverse();
			break;
		case 12:
			mylist.clear();
			break;
		case 13:
			mylist.destroy();
			break;
		case 14:
			mylist.quit_system(input);
			break;
		default:
			break;
		}
	}
	return 0;
}



头删:


技术分享


技术分享


头插:


技术分享


技术分享


反转:


技术分享


技术分享



尾删:


技术分享


尾插:


技术分享


技术分享


按值插入:


技术分享


技术分享


清空:


技术分享


技术分享


排序:


技术分享


技术分享


按值删除:


技术分享


技术分享


【数据结构】双循环链表(c++)

标签:数据结构   c++   双循环链表   

原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/46128555

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