c++实现数据结构4.双循环链表

头文件:


#ifndef _DCLIST_H_
#define _DCLIST_H_

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

typedef enum{TRUE,FALSE}Status;

template<class Type>
class dlist;

template<class Type>
class listnode
{
	friend class dlist<Type>;
public:
	listnode() :data(Type()), next(NULL), proi(NULL)
	{}
	listnode(Type d, listnode<Type>*n = NULL, listnode<Type>*p = NULL) :data(d), next(n), proi(p)
	{}
private:
	Type data;
	listnode<Type>* next;
	listnode<Type>* proi;
};

template<class Type>
class dlist
{
public:
	dlist()
	{
		listnode<Type>* p = _buynode(0);
		first = last = p;
		last->next = first;
		first->proi = last;
		size = 0;
	}
	Status push_back(const Type &x)
	{
		listnode<Type>* s = _buynode(x);
		s->next = first;
		first->proi = s;
		last->next = s;
		s->proi = last;
		last = s;
		size++;
		return TRUE;
	}
	Status push_front(Type x)
	{
		listnode<Type>* s = _buynode(x);
		s->proi = first;
		s->next = first->next;
		first->next->proi = s;
		first->next = s;
		if (last == first)
			last = s;
		size++;
		return TRUE;
	}
	void show_list()
	{
		if (first == last)
			return;
		listnode<Type>*p = first->next;
		cout << "<--";
		while (p != first)
		{
			cout << p->data << "<-->";
			p = p->next;
		}
		cout << endl;
	}
	void pop_back()
	{
		if (first == last)
			return;
		first->proi = last->proi;
		last->proi->next = first;
		delete last;
		last = first->proi;
		size--;
	}
	void pop_front()
	{
		int flag = 0;
		if (first == last)
			return;
		if (last->proi == first)
			flag = 1;
		listnode<Type>*p = first->next;
		first->next = p->next;
		p->next->proi = first;
		if (flag)
			last = first;
		delete p;
		size--;
	}
	Status insert_val(Type key)
	{
		listnode<Type>*s = _buynode(key);
		listnode<Type>*p = first->next;
		while (p->next->data < key && p->next != first)
		{
			p = p->next;
		}
		if (p->next != first)
		{
			s->next = p->next;
			s->next->proi = s;
			s->proi = p;
			p->next = s;
		}
		else
		{
			s->next = first;
			first->proi = s;
			last->next = s;
			s->proi = last;
			last = s;
		}
		size++;
		return TRUE;
	}
	int length()
	{
		return size;
	}
	listnode<Type>* find(Type key)
	{
		if (first == last)
			return NULL;
		listnode<Type>*p = first->next;
		while (p->data != key && p != first)
		{
			p = p->next;
		}
		if (p == first)
			return NULL;
		return p;
	}
	void clear()
	{
		if (first == last)
			return;
		listnode<Type>*p = last;
		while (last != first)
		{
			last = p->proi;
			delete p;
			p = last;
		}
		first->next = last;
		first->proi = last;
		last->next = first;
		last->proi = first;
		size = 0;
	}
	void delete_val(Type key)
	{
		listnode<Type>*d = find(key);
		if (d == NULL)
			return;
		else
		{
			d->proi->next = d->next;
			d->next->proi = d->proi;
			if (d == last)
				last = d->proi;
			delete d;
		}
		size--;
	}
	void sort()
	{
		if (first == last || first->next == last)
			return;
		listnode<Type>* p = first->next;
		listnode<Type>* q = p->next;
		p->next = first;
		first->proi = p;
		last = p;
		while (q != first)
		{
			p = q;
			q = q->next;
			listnode<Type>* m = first;
			while (m->next->data<p->data && m->next != first)
			{
				m = m->next;
			}
			if (m->next != first)
			{
				p->next = m->next;
				p->next->proi = p;
				p->proi = m;
				m->next = p;
			}
			else
			{
				p->next = first;
				first->proi = p;
				last->next = p;
				p->proi = last;
				last = p;
			}
		}
	}
	void resever()
	{
		listnode<Type>* p = first->next;
		listnode<Type>* q = p->next;
		p->next = first;
		first->proi = p;
		last = p;
		while (q != first)
		{
			p = q;
			q = q->next;
			p->next = first->next;
			first->next->proi = p;
			first->next = p;
			p->proi = first;
		}
	}

	Status mesere(dlist<Type>& t1, dlist<Type>& t2)
	{
		if (t1.size==0 && t2.size==0)
			return FALSE;
		t1.sort();
		t2.sort();
		listnode<Type>* p = t1.first->next;
		listnode<Type>* q = t2.first->next;
		listnode<Type>* tmp;
		while (p != t1.first && q != t2.first)
		{
			if (p->data >= q->data)
			{
				tmp = q;
				q = q->next;
				last->next = tmp;
				tmp->proi = last;
				first->proi = tmp;
				last = tmp;
				last->next = first;
				size++;
			}
			else
			{
				tmp = p;
				p = p->next;
				last->next = tmp;
				tmp->proi = last;
				first->proi = tmp;
				last = tmp;
				last->next = first;
				size++;
			}
		}
		while (p != t1.first)
		{
			tmp = p;
			p = p->next;
			last->next = tmp;
			tmp->proi = last;
			first->proi = tmp;
			last = tmp;
			last->next = first;
			size++;
		}
		while (q != t2.first)
		{
			tmp = q;
			q = q->next;
			last->next = tmp;
			tmp->proi = last;
			first->proi = tmp;
			last = tmp;
			last->next = first;
			size++;
		}
		return TRUE;
	}
	void destroy()
	{
		clear();
		delete first;
		first = last = NULL;
	}
	listnode<Type>* proi(Type key)
	{
		listnode<Type>* p = find(key);
		if (p == NULL)
			return NULL;
		return p->proi;
	}
	listnode<Type>* next(Type key)
	{
		listnode<Type>* p = find(key);
		if (p == NULL)
			return NULL;
		return p->next;
	}
protected:
	listnode<Type>* _buynode(Type x)
	{
		listnode<Type> *s = new listnode<Type>(x);
		assert(s != NULL);
		return s;
	}
private:
	listnode<Type>* first;
	listnode<Type>* last;
	size_t         size;
};

#endif


实现部分:
#include"dlist.h"
void main()
{
	dlist<int> mylist;
	dlist<int> t1;
	dlist<int> t2;
	int select = 1;
	int item;
	while (select)
	{
		cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
		cout << "&                                      &" << endl;
		cout << "& [1]  push_back   [2]  push_fornt     &" << endl;
		cout << "& [3]  show_list   [4]  pop_back       &" << endl;
		cout << "& [5]  pop_front   [6]  insert_val     &" << endl;
		cout << "& [7]  length      [8]  find           &" << endl;
		cout << "& [9]  clear       [10] delete_val     &" << endl;
		cout << "& [11] sort        [12] resever        &" << endl;
		cout << "& [13] mesere      [14] destroy        &" << endl;
		cout << "& [15] proi        [16] next           &" << endl;
		cout << "& [0]  quit_system                     &" << endl;
		cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
		cout << "please choose->";
		cin >> select;
		switch (select)
		{
		case 1:
			cout << "please cin data end with -1:" << endl;
			while (cin >> item, item != -1)
			{
				mylist.push_back(item);
			}
			break;
		case 2:
			cout << "please cin data end with -1:" << endl;
			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 << "please cin the data you want insert:";
			cin >> item;
			mylist.insert_val(item);
			break;
		case 7:
			cout << mylist.length() << endl;
			break;
		case 8:
			cout << "please cin the data you want find ,it will back its address: ";
			cin >> item;
			cout << mylist.find(item) << endl;
			break;
		case 9:
			mylist.clear();
			break;
		case 10:
			cout << "please cin the data you want to dalete: ";
			cin >> item;
			mylist.delete_val(item);
			break;
		case 11:
			cout << "sort !" << endl;
			mylist.sort();
			break;
		case 12:
			mylist.resever();
			break;
		case 13:
			cout << "please cin data end with -1:" << endl;
			while (cin >> item, item != -1)
			{
				t1.push_back(item);
			}
			cout << "please cin data end with -1:" << endl;
			while (cin >> item, item != -1)
			{
				t2.push_back(item);
			}
			mylist.mesere(t1, t2);
			mylist.show_list();
			cout << endl;
			break;
		case 14:
			mylist.destroy();
			break;
		case 15:
			cout << "please cin data,return its proi :";
			cin >> item;
			mylist.proi(item);
			break;
		case 16:
			cout << "please cin data,return its next :";
			cin >> item;
			mylist.next(item);
			break;
		default:
			break;
		}
	}
}


双循环链表是链表的精华,举个列子来说,今生错过了,咱们下辈子还可以再续前缘。哈哈,我们是没有这种福气的,而双链表有,他的结构是一个环。
我们可以把它想象称这个样子,在函数实现中要注意某些条件,否则很容易死循环的~_~,
初次学习的可以参考参考,大神请止步!^_^


你可能感兴趣的:(多功能双循环链表)