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

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

循环单链表的实现,和上一篇文章单链表的实现大致相同点击打开链接,略有区别:

1:循环判断的条件不再是s == NULL或者s->next == NULL,而是他们是否等于头指针。2: 断开链表时的处理,尾节点的next不是NULL,而是指向头结点

具体细节参考上一篇文章

头文件:SCList.h

#ifndef SCLIST_H
#define SCLIST_H

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

typedef enum{ FALSE, TRUE }Status;

template<class Type>
class List;

template<class Type>
class ListNode
{
	friend class List<Type>;
private:
	Type data;
	ListNode *next;
public:
	ListNode() :data(Type()), next(NULL){}
	ListNode(Type d, ListNode<Type> *n = NULL) :data(d), next(n){}
	void SetData(Type d){ data = d; }
	Type GetData()const{ return data; }
	~ListNode(){}
};

template<class Type>
class List
{
private:
	ListNode<Type> *first;
	ListNode<Type> *last;
	size_t         size;
public:
	List()
	{
		ListNode<Type> *s = new ListNode<Type>;
		assert(s != NULL);
		first = last = s;
		last->next = first;
	}
	~List()
	{
		destory();
	}
	Status push_back(const Type &x)
	{
		ListNode<Type> *s = new ListNode<Type>(x);
		if (s == NULL)
			return FALSE;
		last->next = s;
		last = s;
		last->next = first;
		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 = new ListNode<Type>(x);
		if (s == NULL)
			return FALSE;
		s->next = first->next;
		first->next = s;
		if (size == 0)
		{
			last = s;
			last->next = first;//---------------->可以省略,think
		}
		size++;
		return TRUE;
	}
	Status pop_back()
	{
		if (size == 0)//
		{
			cout << "循环单链表已空,无法尾删" << endl;
			return FALSE;
		}
		ListNode<Type> *s = first;
		while (s->next != last)
			s = s->next;
		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;
		first->next = s->next;
		delete s;
		if (size == 1)
		{
			last = first;
			last->next = first;//可以省略-------------->
		}
		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)
			push_back(x);
		else
		{
			ListNode<Type> *p = new ListNode<Type>(x);
			assert(p != NULL);
			p->next = s->next;
			s->next = p;
			size++;
		}
		return TRUE;
	}
	ListNode<Type>* find(const Type &x)
	{
		if (size == 0)
			return NULL;
		ListNode<Type> *s = first->next;
		while (s != first)
		{
			if (s->data == x)
				return s;
			s = s->next;
		}
		return NULL;
	}
	Status delete_val(const Type &x)
	{
		ListNode<Type> *s = find(x);
		if (s == NULL)
		{
			cout << "该元素不存在,无法删除" << endl;
			return FALSE;
		}
		if (s == last)
		{
			pop_back();
		}
		else
		{
			ListNode<Type> *p = s->next;
			s->data = p->data;
			s->next = p->next;
			if (p == last)//------------------->>注意
			{
				last = s;
			}
			delete p;
			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;
			//insert_val(s->data);
			//delete s;
			ListNode<Type> *q = first;
			while (q->next != NULL && q->next->data < s->data)
			{
				q = q->next;
			}
			if (q->next == NULL)
			{
				last->next = s;
				last = s;
				last->next = first;
			}
			else
			{
				s->next = q->next;
				q->next = s;
			}
		}
	}
	//从第一个节点处断开,一分为二,头结点和第一个节点成为一个单独的循环单链表
	//将剩余的节点依次进行头插
	void reserve()
	{
		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 = s;
		}
	}
	size_t lenth()
	{
		return size;
	}
	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;
			}
			delete s;
			size--;
			s = first->next;
		}
	}
	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)//第一个节点没有前驱
			return NULL;
		ListNode<Type> *p = first;
		while (p->next != s)
		{
			p = p->next;
		}
		return p;
	}
	void destory()
	{
		clear();
		delete first;
		first = last = NULL;
	}
};
#endif

测试文件:main.cpp

#include"SCList.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] reserve   *" << 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.reserve();
			break;
		case 13:
			cout << "请输入要查找后继的元素:";
			cin >> item;
			//p = mylist.next(item);
			//if (p != NULL)
			//	cout << p->GetData() << endl;
			break;
		case 14:
			mylist.clear();
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}

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

时间: 2024-10-13 01:00:53

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

*循环单链表(不带头结点)

记忆精简:不带头结点,需要创建n个结点,包括三个指针,头指针head,一个游标指针p和创建结点的s.... void creat(List &L,int n) { int e; List s,p; L=NULL; for(int i=1;i<=n;i++)/*n个结点*/ { s=(List)malloc(sizeof(Node)); cin>>e; s->data=e; if(L==NULL) L=s; else p->next=s; p=s; } p->ne

【C语言数据结构】循环单链表

CircleLinkList.h #ifndef CIRCLE_LINK_LIST #define CIRCLE_LINK_LIST //链表节点 typedef struct _CircleLinkListNode {     struct _CircleLinkListNode *next; }CircleLinkListNode; //循环单链表 typedef void CircleLinkList; /*  * 创建循环单链表  * @return 返回循环单链表的指针  */ Cir

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

所实现的单链表的结构如下图所示: 头文件:SList.h #include<iostream> #include<cassert> using namespace std; typedef enum{FALSE,TRUE}Status; template<class Type> class List; template<class Type> class ListNode { friend class List<Type>; //友元类可以访问该

C实现头插法和尾插法来构建单链表(不带头结点)

链表的构建事实上也就是不断插入节点的过程.而节点的插入能够分为头插法和尾插法. 头插法就是在头结点后插入该节点,始终把该节点作为第一个节点.尾插法就是在链表的最后一个节点处插入元素,作为最后一个节点.假设想要了解链表的概念和其它链表操作.请參考<数据结构与算法之链表><C语言实现链表的基本操作>两篇文章.演示样例代码上传至  https://github.com/chenyufeng1991/HeadInsertAndTailInsert . // // main.c // Hea

C实现头插法和尾插法来构建非循环双链表(不带头结点)

在实际使用中,双链表比单链表方便很多,也更为灵活.对于不带头结点的非循环双链表的基本操作,我在<C语言实现双向非循环链表(不带头结点)的基本操作>这篇文章中有详细的实现.今天我们就要用两种不同的方式头插法和尾插法来建立双链表.代码上传至  https://github.com/chenyufeng1991/HeadInsertAndTailInsertDoubleList  . 核心代码如下: //尾插法创建不带头结点的非循环双向链表 Node *TailInsertCreateList(No

C语言实现单链表(不带头结点)节点的插入

对单链表进行增删改查是最主要的操作.我在上一篇博客<C语言实现链表节点的删除>实现了删除单链表中的某个节点. 这里我们要来实如今某个位置插入节点.演示样例代码上传至https://github.com/chenyufeng1991/InsertList  . 核心代码例如以下: Node *InsertToPosition(Node *pNode,int pos,int x){ if (pos < 0 || pos > sizeList(pNode) ) { printf(&quo

单链表(不带头结点)

#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; }LNode,*LinkList; void InitList(LinkList &l); void Create_head(LinkList &l); void Create_rear(LinkList &l); void O

单链表(c++带头结点,)

<pre name="code" class="cpp">#ifndef _SEQLIST_ #define _SEQLIST_ #include<iostream> using namespace std; #include<assert.h> template<class Type> class Slist; //节点类 template<class Type> class Node { public:

数据结构之自建算法库——循环单链表

本文针对数据结构基础系列网络课程(2):线性表中第13课时双链表. 按照"0207将算法变程序"[视频]部分建议的方法,建设自己的专业基础设施算法库. 双链表算法库算法库采用程序的多文件组织形式,包括两个文件: 1.头文件:clinklist.h,包含定义双链表数据结构的代码.宏定义.要实现算法的函数的声明: #ifndef CLINKLIST_H_INCLUDED #define CLINKLIST_H_INCLUDED //循环单链表基本运算函数 typedef int ElemT