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

📄 mylist.h

📁 数据结构中的几种基本结构的C++实现:栈、队列、链表
💻 H
字号:
#include<iostream.h>
const int maxlen=100;
enum Error_code{underflow,overflow,success,unsuccess};
template<class list_type>
class myList{
	int count;
	list_type data[maxlen];
public:
	myList(){count=0;}//无构造函数,无法连接
	bool empty()const;
	bool full()const;
	int size()const;
	void clear();
	//void traverse(void (*visit)(List_type &));
	Error_code retrieve(int pos,list_type &x)const;
	Error_code replace(int pos,const list_type &x);
	Error_code insert(int pos,const list_type &x);
	Error_code remove(int pos,list_type &x);
	Error_code copy(myList &dest,myList &source);
	//Error_code join(List<list_type> &L1,myList &L2);
	//Error_code split(myList<list_type> &source,myList<list_type> &oddmyList,List<list_type> &evenlist);
	void display();
};

/*template<class list_type>
void myList<list_type>::traverse(void (*visit)(list_type &)){
	for(int i=0;i<count;i++)(* visit)(data[i]);
}*/

template <class list_type>
bool myList<list_type>::empty() const{
	if(count==0)return true;
	return false;
}

template <class list_type>
bool myList<list_type>::full() const{
	if(count==maxlen)return true;
	return false;
}
template <class list_type>
int myList<list_type>::size() const{
	return count;
}
template<class list_type>
Error_code myList<list_type>::retrieve(int pos,list_type &x)const{
	if(pos<1||pos>count)return unsuccess;
	x=data[pos-1];
	return success;
}

template<class list_type>
Error_code myList<list_type>::replace(int pos,const list_type &x){
	if(pos<1||pos>count)return unsuccess;
	data[pos-1]=x;
	return success;
}

template<class list_type>
Error_code myList<list_type>::insert(int pos,const list_type &x){
	if(count==0&&pos==0){
		data[0]=x;
		count++;
		return success;
	}
	if(pos<0||pos>count)return unsuccess;
	if(full())return overflow;
	for(int i=count;i>pos;i--)
		data[i]=data[i-1];
	data[pos]=x;
	count++;
	return success;
}

template<class list_type>
Error_code myList<list_type>::remove(int pos,list_type &x){
	
	if(pos<1||pos>count)return unsuccess;
	if(empty())return underflow;
	x=data[pos-1];
	for(int i=pos-1;i<count-1;i++)
		data[i]=data[i+1];
	count--;
	return success;
}

template<class list_type>
Error_code myList<list_type>::copy(myList &dest,myList &source){//not ok
	if(!dest.empty())dest.clear();
	for(int i=0;i<source.size();i++)
		dest.data[i]=source.data[i];
	return success;
}

template<class list_type>
void myList<list_type>::display(){
	cout<<"The members in the List are:"<<endl;
	for(int i=0;i<count;i++)
		cout<<data[i]<<' ';
	cout<<endl;
}

⌨️ 快捷键说明

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