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

📄 property_mgr.h

📁 第一次上传
💻 H
字号:
#include "property_node.h"
#include <iomanip>
template <class mgrtype>
//class product;

class property_mgr{                                                    //带头指针的属性链管理链表
	//friend class product;
	
	
public:
	property_node<mgrtype>* first;                                     //头指针
	property_mgr();                                                    //构造函数
	property_mgr(string n,mgrtype c);                                  //构造函数
	bool isempty();                                                    //链是否为空
	bool add_property(string n,mgrtype c);                             //增加属性结点到链尾
	property_node<mgrtype>* get_prenode(property_node<mgrtype>* p);    //返回p的前一个结点
	bool delete_property(string n);                                    //删除属性名称为n的属性		
	int num_property(string n);                                        //返回属性名称为n的属性的个数	
	void displaynode();                                                //输出所有属性
	mgrtype get_property_mgr(string n);                                 //取得属性名称为n的属性
	property_node<mgrtype>* re_propertynode_name(string n);            //返回名称为n的结点
	
};

template <class mgrtype>
property_mgr<mgrtype>::property_mgr(){
	first=new property_node<mgrtype>();
}

template <class mgrtype>
property_mgr<mgrtype>::property_mgr(string n,mgrtype c){
	first=new property_node<mgrtype>("first",c);
	first->next=new property_node<mgrtype>(n,c);
}

template <class mgrtype>
bool property_mgr<mgrtype>::isempty(){
	if(first->next==NULL)
		return true;
	else
		return false;
}

template <class mgrtype>
int property_mgr<mgrtype>::num_property(string n){
	int i=0;
	property_node<mgrtype>* temp=first->next;	
	if(!isempty()){
		while(1){
			if(temp->get_name_property()==n)
				i++;
			temp=temp->next;
			if(temp==NULL)
				break;
		}
	}
	return i;
}

template <class mgrtype>
property_node<mgrtype>* property_mgr<mgrtype>::get_prenode(property_node<mgrtype>* p){           //返回p的前一个结点
	property_node<mgrtype>* temp=first->next;
	if(!isempty()){
		while(temp->next!=p&&temp->next!=NULL)
			temp=temp->next;
		if(temp->next==p)
			return temp;
		else
			return NULL;
	}
	return first;
}

template <class mgrtype>
bool property_mgr<mgrtype>::add_property(string n,mgrtype c){                                    //增加属性结点
	if(num_property(n)>0){
		return false;
	}
	else{
		property_node<mgrtype>* temp=first->next;
		property_node<mgrtype>* newnode=new property_node<mgrtype>(n,c);
		if(!isempty()){		
			while(temp->next!=NULL)
				temp=temp->next;
			temp->next=newnode;
		}
		else{
			first->next=newnode;
		}
	}
	return true;
}

template <class mgrtype>
bool property_mgr<mgrtype>::delete_property(string n){                                           //删除属性名称为n的属性
	if(isempty())
		return false;
	else if(num_property(n)==0)
		return false;
	else{
		property_node<mgrtype>* temp=first->next;
		while(1){
			if(temp->get_name_property()==n){
				break;
			}
			else
				temp=temp->next;
		}
		property_node<mgrtype>* pre_temp=get_prenode(temp);
		if(pre_temp!=NULL){
			pre_temp->next=temp->next;
			delete temp;
		}
		else
			return false;
	}
	return true;
}

template <class mgrtype>
property_node<mgrtype>* property_mgr<mgrtype>::re_propertynode_name(string n){                   //返回名为n的结点
	if(isempty())
		return NULL;
	else if(num_property(n)==0)
		return NULL;
	else{
		property_node<mgrtype>* temp=first->next;
		while(1){
			if(temp->get_name_property()==n){
				break;
			}
			else
				temp=temp->next;
		}
		return temp;
	}
}

template <class mgrtype>
mgrtype property_mgr<mgrtype>::get_property_mgr(string n){                                       //取得属性名称为n的属性
	property_node<mgrtype>* node=re_propertynode_name(n);
	if(node!=NULL)
		return node->get_content_property();	
}


template <class mgrtype>
void property_mgr<mgrtype>::displaynode(){                                                       //输出所有属性
	if(isempty())
		cout<<"";
	else{
		property_node<mgrtype>* temp=first->next;
		while(temp->next!=NULL){			
			cout<<temp->get_name_property()<<":"<<temp->get_content_property()<<"  ";
			temp=temp->next;
		}
		cout<<temp->get_name_property()<<":"<<temp->get_content_property()<<"  \n";		
	}
}

⌨️ 快捷键说明

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