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

📄 orderlist.h

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

template<class T>
class OrderList
{
	private:
		ListNode<T> *head;
		int size;
		ListNode<T> *currPtr;
    public:
		OrderList(void)
		{
			head=new ListNode<T>();
			size=0;
		};
		~OrderList(void)
		{
			ClearList();
			delete head;
		};

		int ListSize(void)const
		{return size;};
		int EmptyList()const
		{
			if(size<=0)return 1;
			else return 0;
		}
		ListNode<T> *Index(int pos)const;
		void Insert(const T &item);
        T Delete(const T &item);
		T GetData(int pos);
		void ClearList();

		//遍历有序单链表类
		ListNode<T> *Reset(int pos=0);
		ListNode<T> *Next(void);
		int EndOfList(void)const;

};
template<class T>
ListNode<T> *OrderList::Index(int pos)
{
	ListNode<T> *p;
	if(pos<-1 || pos>size)
	{
		cout<<"pos已越界!"<<endl;
		exit(0);
	}
	if(pos==-1)p=head;
	p=head->next;
	int i=0;
	while(p!=NULL && i<pos)
	{
		p=p->next;
		i++;
	}
    return p;
}
template<class T>
void OrderList::Insert(const T &item)
{
    ListNode<T> *p=head;
	for(int i=0;i<size;i++)
	{
		if(item<p->next->data || item=p->next->data)
		{
			ListNode<T> newNode=new ListNode<T>(item,p->next);
			p->next=newNode;
		}
		else p=p->next;//
	}
	size++;
}
template<class T>
T OrderList::Delete(const T &item)
{
	ListNode<T> *p=head,*pl;
	for(int i=0;i<size;i++)
	{
		if(item==p->next->data)
		{
			T data=p->next->data;
			pl=p->next;
			p->next=p->next->next;
			delete pl;
			return data;
		}
	    else p=p->next;
	}
	cout<<"无此数据,无法删除!"<<endl;
}
template<class T>
T OrderList::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 OrderList::ClearList(void)const
{
	ListNode<T> *p,*pl;
	p=head->next;
	while(p!=NULL)
	{
		pl=p;
		p=p->next;
		delete pl;
	}
	size=0;
}
template <class T>
ListNode<T> *OrderList::Reset(int pos)
{
    if(head==NULL)return NULL;
	if(pos<-1 || pos>size-1)
	{
		cout<<"参数出错!"<<endl;
		exit(0);
	}
	if(pos==-1)return head;
	if(pos==0)currPtr=head->next;
	else
	{
		currPter=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> *orderList::Next(void)
{
	currPtr=currPtr->next;
	return currPtr;
}
template<class T>
int OrderList::EndOfList(void)const
{
	if(currPtr=NULL)return 1;
	else 0;
}

⌨️ 快捷键说明

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