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

📄 5.cpp

📁 c++ splitter list function
💻 CPP
字号:
#include <iostream>
using namespace std;

enum errorcode {success,out,underflaw};

template <class t>
struct node
{
	t info;
	node<t> * link;
	node()
	{
		link=0;
	}
	node(t i,node<t>* l=0)
	{
		info=i;
		link=l;
	}
};

template <class t>
class linkedListType
{
	protected:
		node<t> * first;
		node<t> * last;
	public:
		linkedListType();
		bool is_empty();
		void print();
		int size();
		void add_first(t el);
		void add_last(t el);
		void splitAt (linkedListType<t> &newList, t el);
		errorcode add_pos(t el,int pos);
		errorcode delete_first();
		errorcode delete_last();
		errorcode delete_pos(int pos);
		errorcode delete_node(t el);
		node<t> * search(t el);
};


template <class t>
linkedListType<t>::linkedListType()
{
	first=0;
	last=0;
}

template <class t>
bool linkedListType<t>::is_empty()
{
	return (first==0);
}

template <class t>
void linkedListType<t>::print()
{
	node <t>* tmp=first;
	while(tmp!=0)
	{
		cout<<tmp->info<<"  ";
		tmp=tmp->link;
	}
	cout<<endl;
}

template <class t>
int linkedListType<t>::size()
{
	int s=0;
	node<t>* tmp=first;
	while(tmp!=0)
	{
		s++;
		tmp=tmp->link;
	}
	return s;

}


template<class t>
void linkedListType<t>::add_first(t el)
{
	if(is_empty())
		first=last=new node<t>(el);
	else
		first=new node<t>(el,first);
}

template <class t>
void linkedListType<t>::add_last(t el)
{
	if(is_empty())
		first=last=new node<t>(el);
	else
	{
		last->link=new node<t>(el);
		last=last->link;
	}
}

template<class t>
errorcode linkedListType<t>::add_pos(t el,int pos)
{
	if(pos<0 || pos >size())
		return out;
	if(pos==0)
		add_first(el);
	else
		if(pos==size())
			add_last(el);
		else
		{
			node<t>* tmp=first;
			for(int i=0;i<pos-1;i++)
				tmp=tmp->link;
			tmp->link=new node<t>(el,tmp->link);
		}
		return success;
}


template<class t>
errorcode linkedListType<t>::delete_first()
{
	if(is_empty())
		return underflaw;
	if(first==last)
	{
		delete first;
		first=last=0;
	}
	else
	{
		node <t>* tmp=first;
		first=first->link;
		delete tmp;
	}
	return success;
}

template <class t>
errorcode linkedListType<t>::delete_last()
{
	if(is_empty())
		return underflaw;
	if(size()==1)
		delete_first();
	else
	{
		node <t>* tmp=first;
		while(tmp->link!=last)
			tmp=tmp->link;
		delete last;
		last=tmp;
		last->link=0;
	}
	return success;
}

template <class t>
errorcode linkedListType<t>::delete_pos(int pos)
{
	if(is_empty())
		return underflaw;
	if(pos<0 || pos>=size())
		return out;
	if(pos==0)
		delete_first();
	else
		if(pos==size()-1)
			delete_last();
		else
		{
			node<t>*tmp1=first,*tmp2;
			for(int i=0;i<pos-1;i++)
				tmp1=tmp1->link;
			tmp2=tmp1->link;
			tmp1->link=tmp2->link;
			delete tmp2;
		}
		return success;
}

template <class t>
node<t>* linkedListType<t>::search(t el)
{
	node <t> *tmp=first;
	while(tmp!=0)
	{
		if (el==tmp->info)
			return tmp;
		tmp=tmp->link;
	}
		return 0;
}

template<class t>
errorcode linkedListType<t>::delete_node(t el)
{
	if(is_empty())
		return underflaw;
	node<t> *tmp1=first;
	node<t> *tmp2=search(el);
	int i=0;
	int c=-1;
	while (tmp1!=0)
	{
		if (tmp1==tmp2)
			c=i;
		i++;
			tmp1=tmp1->link;
	}
		if (c!=-1)
			delete_pos (c);
		else 
			return out;
		return success;
}


template <class t>
void linkedListType<t>::splitAt (linkedListType<t> &newList,t el)
{
	node<t> * tmp=first;
	while (tmp->info!=el)
	tmp=tmp->link;
	while (tmp!=0)
	{
		t el=tmp->info;
		tmp=tmp->link;
		newList.add_last(el);
		delete_node(el);
	}

}


void main()
{
	linkedListType<int> l;
	l.add_first(5);
	l.add_last(2);
	l.add_first(6);
	l.add_last(5);
	l.add_last(222);
	l.add_pos(10,0);
	l.add_pos(5,2);
	l.add_pos(30,7);
	l.print();
	linkedListType<int> l2;
	l.splitAt(l2,222);
	l2.print();
}

⌨️ 快捷键说明

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