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

📄 list.hpp

📁 本软件实现了模拟cpu的基本工作原理和工作过程
💻 HPP
字号:
#ifndef __List_hpp
#define __List_hpp

#include "Error_code.hpp"
#include "Node2.hpp"

template <class List_entry>
class List{
public:
	List();
	List(const List<List_entry>& other);
	List<List_entry>& operator=(const List<List_entry>& other); 
	~List();

	void clear();
	bool empty()const;
	int size()const;

	Error_code insert(int position,const List_entry& x);//0<=position<=count
	Error_code remove(int position,List_entry& x);//0<=position<count
	Error_code retrieve(int position,List_entry& x);//0<=position<count
	Error_code replace(int position,const List_entry& x);//0<=position<count
	
	void traverse(void (*function)(List_entry&));

protected:
	//受保护成员函数
	void set_position(int position);
	//受保护数据成员
	int count;
	int current_position;//当没有任何元素时,值为-1
	Node2<List_entry>* current;
	Node2<List_entry>* head;	
};


//受保护成员函数
template <class List_entry>
void List<List_entry>::set_position(int position)
{
	for(;current_position<position;current_position++)current=current->next;
	for(;current_position>position;current_position--)current=current->back;
}
//公有成员函数
template <class List_entry>
List<List_entry>::List()
{
	count=0;
	current_position=-1;
	current=NULL;
	head=NULL;
}
template <class List_entry>
List<List_entry>::List(const List<List_entry>& other)
{
	count=other.count;
	

	Node2<List_entry>* ptr1=other.head;
	Node2<List_entry>* ptr2;
	if(ptr1){
		current=new Node2<List_entry>(ptr1->entry);
		head=current;
		ptr1=ptr1->next;
		ptr2=current;
	}
	else head=current=NULL;
	while(ptr1){
		current=new Node2<List_entry>(ptr1->entry,ptr2);
		current->back->next=current;
		ptr1=ptr1->next;
		ptr2=current;
	}
	
	current_position=count-1;
	set_position(other.current_position);
}
template <class List_entry>
List<List_entry>& List<List_entry>::operator=(const List<List_entry>& other)
{
	Node2<List_entry>* ptr1;
	Node2<List_entry>* ptr2;
	while(head){
		ptr1=head;
		head=head->next;
		delete ptr1;
	}

	count=other.count;
	current_position=other.current_position;
	ptr1=other.head;
	if(ptr1){
		current=new Node2<List_entry>(ptr1->entry);
		head=current;
		ptr1=ptr1->next;
		ptr2=current;
	}
	while(ptr1){
		current=new Node2<List_entry>(ptr1->entry,ptr1);
		ptr1=ptr1->next;
		ptr2=current;
	}
	set_position(current_position);	

	return *this;
}
template <class List_entry>
List<List_entry>::~List()
{
	Node2<List_entry>* ptr;
	while(head){
		ptr=head;
		head=head->next;
		delete ptr;
	}
}
///////////////////////////////////////////////////////////////////////////////
template <class List_entry>
void List<List_entry>::clear()
{
	Node2<List_entry>* ptr;
	while(head){
		ptr=head;
		head=head->next;
		delete ptr;
	}
	current=NULL;
	count=0;
	current_position=-1;
}

template <class List_entry>
bool List<List_entry>::empty()const
{
	return (count==0);
}
template <class List_entry>
int List<List_entry>::size()const
{
	return count;
}
///////////////////////////////////////////////////////////////////////////////

template <class List_entry>
Error_code List<List_entry>::insert(int position,const List_entry& x)
{
	if(position<0||position>count)return range_error;
	
	Node2<List_entry>* new_node,*following,*preceding;
	//若在0位置插入
	if(position==0){
		if(count==0)following=NULL;
		else{
			set_position(0);
			following=current;
		}
		preceding=NULL;
	}
	//在非0位置插入
	else{
		set_position(position-1);
		preceding=current;
		following=preceding->next;
	}
	//插入
	new_node=new Node2<List_entry>(x,preceding,following);
	if(!new_node)return overflow;
	if(preceding)preceding->next=new_node;
	if(following)following->back=new_node;
	//修改链表参数
	current=new_node;
	current_position=position;
	if(!position)head=new_node;
	count++;
	return success;
}
template <class List_entry>
Error_code List<List_entry>::remove(int position,List_entry& x)
{
	if(position<0||position>=count)return range_error;
	
	set_position(position);
	x=current->entry;
	Node2 <List_entry>* ptr;
	if(position==0){
		ptr=current;
		head=current=current->next;
		if(current)current->back=NULL;
		else	   current_position-=1;
		delete ptr;
	}
	else{
		current->back->next=current->next;
		if(current->next)current->next->back=current->back;
		ptr=current;
		current=current->back;
		delete ptr;
		current_position--;
	}
	count--;
	return success;
}
template <class List_entry>
Error_code List<List_entry>::retrieve(int position,List_entry& x)
{
	if(position<0||position>=count)return range_error;	
	set_position(position);
	x=current->entry;
	return success;
}
template <class List_entry>
Error_code List<List_entry>::replace(int position,const List_entry& x)
{
	if(position<0||position>=count)return range_error;	
	set_position(position);
	current->entry=x;
	return success;
}
template <class List_entry>
void List<List_entry>::traverse(void (*function)(List_entry&))
{
	Node2<List_entry>* ptr=head;
	while(ptr){
		(*function)(ptr->entry);
		ptr=ptr->next;
	}
			
			
}

#endif

⌨️ 快捷键说明

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