⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cirlist.h

📁 数据结构头文件源代码
💻 H
字号:
#include"ListNode.h"


template <class T>
class CirList
{
private:
	ListNode<T> *head;
	int size;
    ListNode<T> *currPtr;
public:
	CirList(void)
	{
		head=new ListNode<T>() ;
		head->next=head;
		size=0;
	};
	~CirList(void)
	{
		ClearList();
		delete head;
	};
    int ListSize(void)const
	{return size;};
	ListNode<T> *Index(int pos);
	void Insert(const T&item,int pos);
	T Delete(int pos);
	T GetData(int pos);
	int ListEmpty(void)const
    {
        if (size<=0)return 1;
		else return 0;
	};
    void ClearList(void);
    
	//遍历单链表的成员函数
	ListNode<T> *Reset(int pos=0);
	ListNode<T> *Next(void);
	int EndOfList(void)const;

	//新增加的成员函数
	int NextEndOfList()const;
	T DeleteAfter();
};
template<class T>
ListNode<T>* CirList<T>::Index(int pos)
{
	if(pos<-1 ||pos>size)
	{
		cout<<"参数pos的越界!"<<endl;
		exit(0);
	}
	if(pos==-1) return head;
	ListNode<T> *p=head->next;
	int i=0;
	while(p!=head &&i<pos)
	{
		p=p->next;
		i++;
	}
	return p;
}
template <class T>
void CirList<T>::Insert(const T &item,int pos)
{
	if(pos<0||pos>size)
	{
		cout<<"参数pos越界出错!"<<endl;
		exit(0);
	}
	ListNode<T> *p=Index(pos-1);
	ListNode<T> *newNode=new ListNode<T>(item,p->next);
	p->next=newNode;
	size++;
}
template <class T>
T CirList<T>::Delete(int pos)
{
	if(pos<0||pos>size-1)
	{
		cout<<"参数pos越界出错!"<<endl;
		exit(0);
	}
	ListNode<T> *q,*p=Index(pos-1);
	q=p->next;
	p->next=p->next->next;
	T data=q->data;
	delete q;
	size--;
	return data;
}

template<class T>
T CirList<T>::GetData(int pos)
{
	if(pos<0||pos>size-1)
	{
		cout<<"参数pos越界出错!"<<endl;
		exit(0);
	}

	ListNode<T> *p=Index(pos);
	return p->data;
}
template<class T>
void CirList<T>::ClearList(void)
{
	ListNode<T>*p, *pl;
	p=head->next;
	while(p!=head)
	{
		pl=p;
		p=p->next;
		delete pl;
	}
	size=0;
}
template<class T>
ListNode<T>* CirList<T>::Reset(int pos)
{
	if(head==NULL)return NULL;
	if(pos<-1||pos>=size)
	{
		cout<<"参数出错!"<<endl;
		exit(0);
	}
	if(pos==-1)return head;
	if(pos==0)currPtr=head->next;
	else
	{
		currPtr=head->next;
		ListNode<T>prevPtr=head;
		for(int i=0;i<pos;i++)
		{
			prevPtr=currPtr;
			currPtr=currPtr->next;
		}
	}
	return currPtr;
}
template<class T>
ListNode<T> * CirList<T>::Next(void)
{
	currPtr=currPtr->next;
	return currPtr;
}
template<class T>
int  CirList<T>::EndOfList(void)const  //currPrt判断是否指在链表尾是返回1否返回0
{
	if(currPtr==head)return 1;
	else return 0;
}
template<class T>
int CirList<T>::NextEndOfList()const
{
	if(currPtr->next==head) return 1;
	else return 0;
}
template<class T>
T CirList<T>::DeleteAfter()
{
	ListNode<T> *p=currPtr->next;
	currPtr->next=p->next;
	T data=p->data;
	delete p;
	size--;
	return data;
}

     
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -