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

双循环链表(C++)

时间:2015-08-29 09:48:41      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:双链表   循环   前驱   后继   

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

template <class Type>class DCList;

template<class Type>  
class Node
{
	friend class  DCList<Type>;
public:
	Node():data(0),prio(NULL),next(NULL){}
	Node(Type d,Node<Type>*p=NULL,Node<Type>*n=NULL) :data(d), prio(p), next(n)
	{}
	~Node(){}
	void SetData(Type d)
	{
		data = d;
	}
	Type GetData()const
	{
		return data;
	} 
private:
	Node<Type>* prio;
	Node<Type>* next;
	Type        data;
};
template <class Type> 
class DCList
{
public:
	DCList()
	{
		Node<Type>*s = _Buynode(0);
		first = last=s;
		first->prio = last;
		last->next = first;
		size = 0;
	}
	~DCList(){}

	Node<Type>* _Buynode(const Type&x)
	{
		Node<Type>*s = new Node<Type>(x);
		assert(s != NULL);
		return s;
	}
public:
	bool push_back(const Type&x)
	{
		Node<Type>* s = _Buynode(x);
		s->prio = last;
		last->next = s;
		last = s;
		s->next = first;
		first->prio = last;
		size++;
		return true;
	}
	bool push_front(const Type &x)
	{ 
		if (size == 0)
			push_back(x);
		Node<Type>* s = _Buynode(x);
		s->next = first->next;
		first->next->prio = s;
		first->next = s;
		s->prio = first;
		size++;
		return true;
	}
	void show_list()
	{
		if (size == 0)
			return;
		Node<Type>*p = first;
		while (p->next != first)
		{
			p = p->next;
			cout << p->data << "<==>";
		}
		cout << "ok!" << endl;
	}
	void pop_back()
	{
		if (size == 0)
			return;
		Node<Type>*p = last;
		last = last->prio;
		last->next = first;
		first->prio = last;
		delete p;
		size--;
	}
	
	void pop_front()
	{
		if (size == 0)
			return;
		if (size == 1)
			pop_back();
		else
		{
			Node<Type>*p = first->next;
			first->next = p->next;
			p->next->prio = first;
			delete p;
			size--;
		}
	}
	bool insert_val(const Type&x)
	{
		if (size == 0)
			push_back(x);
		else
		{
			Node<Type>*p = _Buynode(x);
			Node<Type>*q = first->next;
			while (q != first && q->data < p->data)
			{
				q = q->next;
			}
			if (q == first)
			{
				push_back(x);
			}
			else
			{
				p->prio = q->prio;
				q->prio->next = p;
				p->next = q;
				q->prio = p;
				size++;
			}
		}
		return true;
	}
	Node<Type>* find(const Type&x)
	{
		if (size == 0)
		{
			cout << "链表为空" << endl;
			return NULL;
		}
		Node<Type>*q = _Buynode(x);
		Node<Type>*p = first->next;
		while (p != first && q->data != p->data)
			p = p->next;
		if (p  == first)
		{
			//cout << "未能在链表中找到" << x <<endl;
			return NULL;
		}
		return p;
	}
	bool delete_val(const Type&x)
	{
		if (size == 0)
			return false;
		Node<Type>*p = find(x);
		if (p == NULL)
		{
			cout << "链表中不存在" << x << "无法删除" << endl;
			return false;
		}
		if (p == last)
		{
			pop_back();
		}
		else
		{
			p->prio->next = p->next;
			p->next->prio = p->prio;
			delete p;
			size--;
		}
		return true;
	}
	void sort()
	{
		if (size == 0 || size == 1)
			return;
		Node<Type>*s = first->next;
		Node<Type>*p = s->next;
		last = s;
		s->next = first;
		first->prio = s;
		while (p != first)
		{
			s = p;
			p = p->next;
			Node<Type>*q = first->next;
			while (q != first && q->data < s->data)
			{
				q = q->next;
			}
				if (q == first)
					push_back(s->data);
				else
				{
					s->prio = q->prio;
					q->prio->next = s;
					s->next = q;
					q->prio = s;
				}
			
		}
	}
	
	void resver()
	{
		if (size == 0 || size == 1)
			return;
		Node<Type>*s = first->next;
		Node<Type>*p = s->next;
		last = s;
		s->next = first;
		first->prio = s;
		while (p != first)
		{
			s = p;
			p = p->next;
			push_front(s->data);
		}
	}
	int length(){ return size; }
bool  next(const Type&x)
	{
		if (size == 0)
			return false;
		Node<Type>*p;
		p=find(x);
		
		if (p == NULL)
		{
			cout << "链表中不存在" << x << endl;
			return false;
		}
		if (p == last)
		{
			cout << x << "的后继是头结点"<< endl;
			//return true;
		}
		else
		{
			cout << x << "的后继是" << p->next->data << endl;
		}
		return true;
}bool  prio(const Type&x)
{
	if (size == 0)
		return false;
	Node<Type>*p;
	p = find(x);

	if (p == NULL)
	{
		cout << "链表中不存在" << x << endl;
		return false;
	}
	if (p == first->next)
	{
		cout << x << "的前驱是头结点" << endl;
		//return true;
	}
	else
	{
		cout << x << "的前驱是" << p->prio->data << endl;
	}
	return true;
}
void clear()
{
	if (size == 0)
		return;
 
	Node<Type>*p = first->next;
	while (p != first)
	{ 
		//Node<Type>*q = p;
		//p = p->next;
		first->next = p->next;
		p->next->prio = first;
		delete p;
		p = first->next;
		size--;
	}
	last = first ;//first=last再次开辟空间会少开辟
	size = 0;
}
private:
	Node<Type>* first;
	Node<Type>* last;
	size_t       size;
};
#endif
#include"DCList.h"

void main()
{
	DCList<int> mylist;

	int Item;
	int select = 1;
	Node<int>* p;
	while (select)
	{
		cout << "************************************" << endl;
		cout << "* [1] push_back     [2] push_front *" << endl;
		cout << "* [3] show_list     [4] pop_back   *" << endl;
		cout << "* [5] pop_fornt     [6] insert_val *" << endl;
		cout << "* [7] length        [8] find       *" << endl;
		cout << "* [9] merge         [10] delete_val*" << endl;
		cout << "* [11] sort         [12] resver    *" << endl;
		cout << "* [13] next         [14] prio      *" << endl;
		cout << "* [15] clear        [0] quit_system*" << endl;
		cout << "************************************" << endl;
		cout << "请选择服务项目:>";
		cin >> select;
		switch (select)
		{
		case 1:
			cout << "请输入要插入的数据(-1结束):>";
			while (cin >> Item, Item != -1)
			{
				mylist.push_back(Item);
			}
			break;
		case 2:
			cout << "请输入要插入的数据(-1结束):>";
			while (cin >> Item, Item != -1)
			{
				mylist.push_front(Item);
			}
			break;
		case 3:
			mylist.show_list();
			break;
		case 4:
			mylist.pop_back();
			break;
		case 5:
			mylist.pop_front();
			break;
		case 6:
			cout << "请输入要插入的值:>";
			cin >> Item;
			mylist.insert_val(Item);
			break;
		case 7:
			cout << "顺序表的长度为:>" << mylist.length() << endl;
			break;
		case 8:
			cout << "请输入要查找的值:>";
			cin >> Item;
			p = mylist.find(Item);
			if (p == NULL)
			{
					cout << "要查找的数据不存在." << endl;
			}
			break;
		case 9:
			break;
		case 10:
			cout << "请输入要删除的值:>";
			cin >> Item;
			mylist.delete_val(Item);
			break;
		case 11:
			mylist.sort();
			break;
		case 12:
			mylist.resver();
			break;
		case 13:
			cout << "请输入要查后继的数";
			cin >> Item;
			mylist.next(Item);
			break;
		case 14:
			cout << "请输入要查前驱的数";
			cin >> Item;
			mylist.prio(Item);
			break;
		case 15:
			mylist.clear();
			break;
		default:
			break;
		}
	}
	system("pause");
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

双循环链表(C++)

标签:双链表   循环   前驱   后继   

原文地址:http://blog.csdn.net/fujinlong520/article/details/48084813

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