c++实现数据结构3.双链表

头文件:list.h
*******************************************************************************************************************************************************************************************************
#ifndef _DLIST_H_
#define _DLIST_H_

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

typedef enum{ TRUE, FALSE }Status;

template<class Type>
class list;

template<class Type>
class listnode
{
	friend class list<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 list
{
public:
	list()
	{
		listnode<Type>* p = _buynode(0);
		first = last = p;
		size = 0;
	}
	Status push_back(const Type &x)
	{
		listnode<Type>* s = _buynode(x);
		last->next = s;
		s->proi = last;
		last = s;
		size++;
		return TRUE;
	}
	Status push_front(Type x)
	{
		listnode<Type>* s = _buynode(x);
		if (first == last)
		{
			s->proi = first;
			first->next = s;
			last = s;
		}
		else
		{
			s->proi = first;
			first->next->proi = s;
			s->next = first->next;
			first->next = s;
		}
		size++;
		return TRUE;
	}
	void show_list()
	{
		if (first == last)
			return;
		listnode<Type>*p = first->next;
		while (p != NULL)
		{
			cout << p->data << "-->";
			p = p->next;
		}
		cout << "END" << endl;
	}
	void pop_back()
	{
		if (first == last)
			return;
		last = last->proi;
		delete last->next;
		last->next = NULL;
		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)
	{
		if (first == last)
		{
			push_back(key);
		}
		else
		{
			listnode<Type>*s = _buynode(key);
			listnode<Type>*p = first->next;
			while (p->next != NULL && p->next->data<key)
			{
				p = p->next;
			}
			if (p->next != NULL)
			{
				s->next = p->next;
				s->next->proi = s;
				s->proi = p;
				p->next = s;
			}
			else
			{
				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 != NULL && p->data != key)
		{
			p = p->next;
		}
		if (p == NULL)
			return NULL;
		return p;
	}
	void clear()
	{
		if (first == last)
			return;
		listnode<Type>*p = last;
		while (last != first)
		{
			last = p->proi;
			delete p;
			p = last;
		}
		last->next = NULL;
		last->proi = NULL;
		first->next = NULL;
		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 (size == 0 || size == 1)
			return;
		listnode<Type>* p = first->next;
		listnode<Type>* q = p->next;
		last = p;
		last->next = NULL;
		while (q != NULL)
		{
			p = q;
			q = q->next;
			listnode<Type>* m = first;
			while (m->next != NULL && m->next->data<p->data)
			{
				m = m->next;
			}
			if (m->next != NULL)
			{
				p->next = m->next;
				p->next->proi = p;
				p->proi = m;
				m->next = p;
			}
			else
			{
				last->next = p;
				p->proi = last;
				last = p;
				last->next = NULL;
			}
		}
	}
	void resever()
	{
		if (size == 0 || size == 1)
			return;
		listnode<Type>* p = first->next;
		listnode<Type>* q = p->next;
		last = p;
		last->next = NULL;
		while (q != NULL)
		{
			p = q;
			q = q->next;
			p->next = first->next;
			first->next->proi = p;
			first->next = p;
			p->proi = first;
		}
	}

	Status merge(list<Type>& t1, list<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 != NULL && q != NULL)
		{
			if (p->data >= q->data)
			{
				tmp = q;
				q = q->next;
				last->next = tmp;
				tmp->proi = last;
				last = tmp;
				size++;
			}
			else
			{
				tmp = p;
				p = p->next;
				last->next = tmp;
				tmp->proi = last;
				last = tmp;
				size++;
			}
		}
		while (p != NULL)
		{
			tmp = p;
			p = p->next;
			last->next = tmp;
			tmp->proi = last;
			last = tmp;
			size++;
		}
		while (q != NULL)
		{
			tmp = q;
			q = q->next;
			last->next = tmp;
			tmp->proi = last;
			last = tmp;
			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;
		cout << "value:" << p->next->data << " adress:";
		return p->proi;
	}
	listnode<Type>* next(Type key)
	{
		listnode<Type>* p = find(key);
		if (p == NULL)
			return NULL;
		cout << "value:" << p->next->data << " adress:";
		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"list.h"
void main()
{
	list<int> mylist;
	list<int> t1;
	list<int> t2;
	int select = 1;
	int item;
	while (select)
	{
		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] merge       [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.merge(t1, t2);
			mylist.show_list();
			cout << endl;
			break;
		case 14:
			mylist.destroy();
			break;
		case 15:
			cout << "please cin data,return its proi :";
			cin >> item;
			cout << mylist.proi(item) << endl;
			break;
		case 16:
			cout << "please cin data,return its next :";
			cin >> item;
			cout << mylist.next(item) << endl;
			break;
		default:
			break;
		}
	}
}

*******************************************************************************************************************************************************************************************************

双链表既可以从前往后跑,又可以从后面往前跑,相对于单链表来说性能上好了很多。


c++实现数据结构3.双链表_第1张图片


这是运行的部分截图,其他的大家可以写一下,开玩笑的,我都实现了^_^,没有必要都截图下来,
大家可以把代码放到相应平台上运行一下,我的平台是vs2013,我测试了,可以移植到linux平台上,
vc++6.0更是可以的。虽然这不是很难的代码,希望在大家需要的时候能节约大家一些时间。


你可能感兴趣的:(c++高效双链表,多种功能绝对能满足你的要求)