双循环链表(C++)

#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");
}

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

时间: 2024-10-15 21:43:30

双循环链表(C++)的相关文章

双循环链表(包含头指针与尾指针)

双循环链表(包含头指针与尾指针) 以及基本功能的实现 list_ d_link_c.h #ifndef _LIST_D_LINK_C_ #define _LIST_D_LINK_C_ #include<iostream> #include<assert.h> using namespace std; #define ElemType int typedef struct Node { ElemType data; struct Node *prio; struct Node *ne

编程算法 - 有序双循环链表的插入 代码(C)

有序双循环链表的插入 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 有序双循环链表的插入, 需要找到插入位置, 可以采用, 两个指针, 一个在前, 一个在后. 保证前面的小于等于插入值, 后面的大于等于插入值. 特殊情况, 首尾插入(大于或小于整个链表)或单节点, 判断条件为后指针指向首节点. 则需要直接插入. 插入链表头, 需要调整链表头节点. 代码22行. 代码: /* * main.cpp * * Created on: 2014.9.18

循环链表之双循环链表

接着上一篇博文http://12172969.blog.51cto.com/12162969/1918256,把循环链表里的双循环链表的基本操纵按照我个人的理解进行总结一下. 依然沿袭过去的风格,进入双循环链表之前,先提另一种结构,双向链表,先有了双向链表再有了双循环链表.这两种结构和单链表一样都有带头结点和不带头结点之分.我们先来看一下这几种结构的结构图: 双链表 双循环链表 既然单向链表有普通的链表也就是不循环的链表.那么双向链表也一样,也有普通的双向链表和双向循环链表:也有带头结点,不带头

【数据结构】双循环链表(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, NodeLi

数据结构之—线性表之—双向链表之—浅谈双循环链表

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都构造双向循环链表. /********************************************************************** * Copyright (c)2015,WK Studios * Filename: Tw_Node.c * Compiler: GCC,VS,VC6.

【数据结构】用C++实现双循环链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)

//[数据结构]用C++实现单循环链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等) //头文件 #ifndef _CDLIST_H #define _CDLIST_H #include<iostream> using namespace std; template<class Type> class CDList; template<class Type> class ListNode { friend class CDList<Type>; p

C语言数据结构——双循环链表的插入操作顺序

双向链表与单链表的插入操作的区别 双向链表因为存在前驱指针和后继指针所以需要修改的指针多于单链表,但指针改动的顺序同样重要 单链表的插入 eg:在节点p的后面插入指针s s->next=p->next;//首先要使要插入的指针指向p->next p->next=s;//再将p的后继指向插入的s即可 注意! 顺序不能调换,否则在将p->next指向s后,原来由p->next指向的节点将会迷失在内存中,很难找到! 双向循环链表的插入 eg:将新的节点插入p节点的后面 s-&

双循环链表遍历

void CPage1::RenWuBL(DWORD base) { DWORD pFirstNode=base; wchar_t *name; char * aa=""; _try { DWORD pNextNode = pFirstNode; while (pFirstNode != *(DWORD *)pNextNode) { name=GetAroundName(pNextNode); USES_CONVERSION; aa=W2A((LPCWSTR)name); TRACE(

双循环链表

1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define OK 1 5 #define ERROR 0 6 7 typedef int Elemtype; 8 typedef int Status; 9 10 typedef struct Node{ 11 Elemtype data; 12 struct Node* prior; 13 struct Node* next; 14 }Node; 15 typedef struct No