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

📄 linlist.h

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


template <class T>
class LinList
{
	private:
		ListNode<T> *head;
		int size;
		ListNode<T> *currPtr;
	public:
		LinList(void)
		{
			head=new ListNode<T>();
			size=0;
		};
		~LinList(void)
		{
			ClearList();
			delete head;
		};
		//线性表的操作要求的成员函数
		int ListSize(void)const
		{return size;};
		int ListEmpty(void)const
		{
			if(size<=0)return 1;
			else return 0;
		};
		ListNode<T> *Index(int pos);
		void Insert(const T&item,int pos);
		T Delete(int pos);
		T GetData(int pos);
		void ClearList(void);
        
		//遍历单链表的成员函数
		ListNode<T> *Reset(int pos=0);
		ListNode<T> *Next(void);
		int EndOfList(void)const;
};
template<class T>
ListNode<T> *LinList<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!=NULL && i<pos)
	{
		p=p->next;
		i++;
	}
	return p;
}

template <class T>
void LinList<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 LinList<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 LinList<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 LinList<T>::ClearList(void)
{
	ListNode<T> *p, *pl;
	p=head->next;
	while(p!=NULL)
	{
		pl=p;
		p=p->next;
		delete pl;
	}
	size=0;
}

template<class T>
ListNode<T> *LinList<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> *LinList<T>::Next(void)
{
	if(currPtr!=NULL)
		currPtr=currPtr->next;
	return currPtr;
}
template<class T>
int LinList<T>::EndOfList(void)const
{
	if(currPtr==NULL)return 1;
	else return 0;
}

⌨️ 快捷键说明

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