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

📄 homework2.cpp

📁 请编写一个程序
💻 CPP
字号:
#include<iostream.h>
//单链表节点类
template<class ElemType>class List;// 单链表类的向前说明
template<class ElemType>class ListItr;// 单链表迭代器类的向前说明
template<class ElemType>class ListNode{
	friend class List<ElemType>;// 单链表类为其友元类, 便于访问结点类中的私有成员
	friend class ListItr<ElemType>; //单链表迭代器类为其友元类, 便于访问结点类中的私有成员
	private:
		ListNode<ElemType> *Next; // 指向下一个结点的指针。
		ElemType Element;// 结点数据。
	public:
		ListNode(const ElemType &E,ListNode<ElemType> *N=NULL)
			:Element(E),Next(N){}// 构造函数
		ListNode():Next(NULL){}// 析构函数
		~ListNode(){}
};
//单链表类
template<class ElemType>class List{
	friend class ListItr<ElemType>;    // 单链表迭代器类为其友元类
	private:
		ListNode<ElemType> *head;// 指向头结点的指针
	public:
		List(){head=new ListNode<ElemType>();}
		~List(){MakeEmpty();delete head;}// 析构函数
		const List &operator = (const List&R);// 完成复制功能。
		int IsEmpty()const{return head->Next==NULL;}
		int IsFull()const {return 0;}
		void MakeEmpty();
	    List& operator ^ (List<ElemType> &);//求两个的交集
};
template<class ElemType>
const List<ElemType>&List<ElemType>::operator = (const List<ElemType>&R){
	if(this==&R) return *this;      // 同一单链表,不必赋值。
	MakeEmpty();	// 清空单链表。
	ListItr<ElemType>Itr(*this);
	for(ListItr<ElemType>Ritr(R);+Ritr;Ritr++) Itr.Insert(Ritr());
	return *this;// 根据单链表R 的结点数据值,创建新结点,并插入到当前链表
}
template<class ElemType>
List<ElemType> & List<ElemType>::operator ^ (List<ElemType> &l)//求交集
{
	ListItr<ElemType> listitr1(*this),listitr2(l);
	ElemType a,b;
	for(;+listitr1&&+listitr2;){
		a=listitr1();b=listitr2();
		if(a>b){				//a大时		
			listitr2++;
			listitr2.Remove(b);
		}
		else 
			if(a<b){
//               listitr1++;
			   listitr1.Remove(a);   //a小时

		}
		else{		           //相等时
			listitr1++;
			listitr2++;
		}
	}
	for(;+listitr1;){//a为最后一个数据,而b都比它小时
		a=listitr1();
        listitr1++;
		listitr1.Remove(a);
	}
    return *this;
}

template<class ElemType>
void List<ElemType>::MakeEmpty(){
	ListNode<ElemType> *Ptr;
	ListNode<ElemType> *NextNode;
	for(Ptr=head->Next;Ptr!=NULL;Ptr=NextNode)
	{
		NextNode=Ptr->Next;
		delete Ptr;
	}
	head->Next=NULL;
}

template<class ElemType>
ostream &operator<<(ostream & Out,const List<ElemType> &L){
	if(L.IsEmpty()) Out<<"Empty List!";
	else for(ListItr<ElemType>Itr(L);+Itr;++Itr) Out<<Itr()<<endl;
	return Out;
}
template<class ElemType>
istream &operator>>(istream & is, List<ElemType> &L)
{
	ElemType a;
	ListItr<ElemType> Itr(L);
    while(is>>a,a!=-1) Itr.Insert(a);
	return is;
}
template<class ElemType>class ListItr{
private:
	ListNode<ElemType> * const head;// 指向头结点的常指针
	ListNode<ElemType> * Current; // 指向当前结点的指针
public:
	ListItr(const List<ElemType>&L):head(L.head)
	{Current=L.IsEmpty()?head:head->Next;}
	~ListItr(){} // 析构函数
	int Find(const ElemType &x);      // 查找值为x的结点,查找成功则使其成为当前结点,并返回True
	int IsFound(const ElemType &x) const;// 查找值为x的结点,查找成功返回True,否则返回False;不改变指针Current的值
	void Insert(const ElemType &x);  // 插入成功,新结点成为当前结点
	int Remove(const ElemType &x);  // 删除值为x的结点的操作
	int operator+()const
	{return Current && Current!=head;}  //当前结点非空则返回Ture
	const ElemType& operator()()const;  //取当前结点的数据值
	void operator++();   	// 使当前结点的直接后继结点成为当前结点 前缀++运算符
    void operator++(int){operator++();}  // 将后缀运算符++定义为前缀++运算符
	void Zeroth(){Current=head;}// 当前指针指向头结点
	void First();   // 当前指针指向首结点
	const ListItr &operator = (const ListItr &);// 赋值运算符
};
template<class ElemType>
void ListItr<ElemType>::Insert(const ElemType&x){
	ListNode<ElemType> *p;
	p=new ListNode<ElemType>(x,Current->Next);
	Current=Current->Next=p;
}
template<class ElemType>
int ListItr<ElemType>::Find(const ElemType &x){
	ListNode<ElemType> * Ptr=head->Next;
    while(Ptr!=NULL&&!(Ptr->Element==x)) Ptr=Ptr->Next;
	if(Ptr==NULL)return 0;
	Current=Ptr;
	return 1;
}
template<class ElemType>
int ListItr<ElemType>::IsFound(const ElemType &x) const{
	ListNode<ElemType> *Ptr=head->Next;
	while(Ptr!=NULL&&!(ptr->Element==x)) Ptr=Ptr->Next;
	return Ptr!=NULL;
}
template<class ElemType>
int ListItr<ElemType>::Remove(const ElemType&x){
    ListNode<ElemType>*Ptr=head;
    while(Ptr->Next!=NULL&&!(Ptr->Next->Element==x))
		Ptr=Ptr->Next;
	if(Ptr->Next==NULL)
		return 0;
	ListNode<ElemType> *p=Ptr->Next;
	Ptr->Next=Ptr->Next->Next;
	delete p;
	return 1;
}
template<class ElemType>
const ElemType &ListItr<ElemType>::operator ()()const{
	return Current->Element;
}
template<class ElemType>
void ListItr<ElemType>::operator ++ (){
	Current=Current->Next;
}
template<class ElemType>
const ListItr<ElemType>&ListItr<ElemType>::operator = (const ListItr<ElemType>&R){
	if(this==&R) return *this;
	Current=R.Current;
	return *this;
}
template<class ElemType>
void ListItr<ElemType>::First(){
	Current=head->Next;
}

int main()
{
	List<int> a,b;
	cout<<"Input two lists and -1 to stop:";
	cin>>a;
	cin>>b;
	cout<<"The same:";
	cout<<(a^b);
	return 0;
}

⌨️ 快捷键说明

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