【c++版数据结构】之循环双链表的实现(带头结点以及尾节点)

所实现的循环双链表的结构如下图所示:

循环双链表的实现,和第一篇文章单链表的实现思想大致相同点击打开链接具体思想参考该文章。本篇文章在构建节点的同时,初始化构建节点的前驱和后继,具体细节参考下列代码

头文件:DCList.h

#ifndef DCLIST_H
#define DCLIST_H

#include<iostream>
#include<cstdlib>
#include<cassert>
using namespace std;

typedef enum{FALSE,TRUE}Status;

template<class Type>
class List;

template<class Type>
class ListNode
{
	template<class Type>
	friend class List;
private:
	Type data;
	ListNode<Type> *prio;
	ListNode<Type> *next;
public:
	ListNode() :data(Type()),prio(NULL), next(NULL){}
	ListNode(const Type x, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL)
		:data(x), prio(p), next(n){}
	Type getData()const { return data; }
};

template<class Type>
class List
{
private:
	ListNode<Type> *first;
	ListNode<Type> *last;
	size_t          size;
public:
	ListNode<Type>* _BuyNode(const Type x, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL)
	{
		ListNode<Type> *s = new ListNode<Type>(x, p, n);
		assert(s != NULL);
		return s;
	}
	List()
	{
		ListNode<Type> *s = _BuyNode(0, NULL, NULL);
		first = last = s;
		last->next = first;
		size = 0;
	}
	~List()
	{
		destory();
	}
	Status push_back(const Type &x)
	{
		ListNode<Type> *s = _BuyNode(x, last, first);
		last->next = s;
		last = s;
		size++;
		return TRUE;
	}
	void show_list()
	{
		ListNode<Type> *s = first->next;
		while (s != first)
		{
			cout << s->data << "->";
			s = s->next;
		}
		cout << "Nul." << endl;
	}
	Status push_front(const Type &x)
	{
		ListNode<Type> *s = _BuyNode(x, first, first->next);
		if (size == 0)
		{
			first->next = s;
			last = s;
			last->next = first;
		}
		else
		{
			first->next->prio = s;
			first->next = s;
		}
		size++;
		return TRUE;
	}
	Status pop_back()
	{
		if (size == 0)
		{
			cout << "循环双链表已空,无法尾删" << endl;
			return FALSE;
		}
		ListNode<Type> *s = last->prio;
		delete last;
		last = s;
		last->next = first;
		size--;
		return TRUE;
	}
	Status pop_front()
	{
		if (size == 0)
		{
			cout << "循环双链表已空,无法头删" << endl;
			return FALSE;
		}
		ListNode<Type> *s = first->next;
		if (size == 1)
		{
			last = first;
			last->next = first;
		}
		else
		{
			first->next = s->next;
			s->next->prio = first;
		}
		delete s;
		size--;
		return TRUE;
	}
	Status insert_val(const Type &x)
	{
		ListNode<Type> *s = first;
		while (s->next != first && s->next->data < x)
		{
			s = s->next;
		}
		if (s->next == first)//直接尾插即可
		{
			ListNode<Type> *p = _BuyNode(x, last, first);
			last->next = p;
			last = p;
		}
		else//找到了前驱,在其之后插入即可
		{
			ListNode<Type> *p = _BuyNode(x, s, s->next);
			s->next->prio = p;
			s->next = p;
		}
		size++;
		return TRUE;
	}
	size_t lenth()
	{
		return size;
	}
	ListNode<Type>* find(const Type &x)
	{
		ListNode<Type> *s = first->next;
		while (s != first && s->data != x)
		{
			s = s->next;
		}
		if (s == first)
			return NULL;
		else
			return s;
	}
	Status delete_val(const Type &x)
	{
		ListNode<Type> *s = find(x);
		if (s == NULL)
		{
			cout << "该元素不存在,无法删除" << endl;
			return FALSE;
		}
		if (s == last)
		{
			ListNode<Type> *p = last->prio;
			delete last;
			last = p;
			last->next = first;
		}
		else
		{
			ListNode<Type> *q = s->next;
			s->data = q->data;
			s->next = q->next;
			q->next->prio = s;
			delete q;
		}
		size--;
		return TRUE;	
	}
	void sort()
	{
		if (size == 0 || size == 1)
			return;
		ListNode<Type> *s = first->next;
		ListNode<Type> *p = s->next;
		last = s;
		last->next = first;
		while (p != first)
		{
			s = p;
			p = p->next;

			ListNode<Type> *q = first;
			while (q->next != first && q->next->data < s->data)
			{
				q = q->next;
			}
			if (q->next == first)
			{
				last->next = s;
				s->prio = last;
				last = s;
				last->next = first;
			}
			else
			{
				s->next = q->next;
				q->next->prio = s;
				q->next = s;
				s->prio = q;
			}
		}
	}
	void reverse()
	{
		if (size == 0 || size == 1)
			return;
		ListNode<Type> *s = first->next;
		ListNode<Type> *p = s->next;
		last = s;
		last->next = first;
		while (p != first)
		{
			s = p;
			p = p->next;

			s->next = first->next;
			first->next->prio = s;
			first->next = s;
			s->prio = first;
		}
	}
	void clear()
	{
		if (size == 0)
			return;
		ListNode<Type> *s = first->next;
		while (s != first)
		{
			if (size == 1)
			{
				last = first;
				last->next = first;
			}
			else
			{
				first->next = s->next;
				s->next->prio = first;
			}
			delete s;
			size--;
			s = first->next;//为下一次删除做准备
		}
	}
	void destory()
	{
		clear();
		delete[] first;
		first = last = NULL;
	}
	ListNode<Type>* next(ListNode<Type> *s)
	{
		if (s == last)
			return NULL;
		else
			return s->next;
	}
	ListNode<Type>* prio(ListNode<Type> *s)
	{
		if (s == first->next) //-------------->第一个节点没有前驱,返回NULL
			return NULL;
		else
			return s->prio;
	}
};
#endif
测试文件main.cpp

#include"DCList.h"
int main()
{
	List<int> mylist;
	int item;
	int n;
	int select = 1;
	//ListNode<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_front           [6] insert_val *" << endl;
		cout << "*[7] lenth               [8] find       *" << endl;
		cout << "*[9] merge               [10] delete_val*" << endl;
		cout << "*[11] sort               [12] reverse   *" << endl;
		cout << "*[13] next               [14] clear     *" << endl;
		cout << "*[15] prio               [0] quit_system*" << 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.lenth() << endl;
			break;
		case 8:
			cout << "请输入要查找的元素:";
			cin >> item;
			if (mylist.find(item))
				cout << "it's found" << endl;
			else
				cout << "it's not exist" << endl;
			break;
		case 9:
			cout << "请输入要删除的位置:";
			cin >> n;
			//mylist.delete_pos(n,item);
			break;
		case 10:
			cout << "请输入要删除的元素:";
			cin >> item;
			mylist.delete_val(item);
			break;
		case 11:
			mylist.sort();
			break;
		case 12:
			mylist.reverse();
			break;
		case 14:
			mylist.clear();
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值