C++线性表操作

#include<iostream>
using namespace std;
class ChainNode {
    friend class ChainIterator;
	friend class Chain;
	private:
		int date;
		ChainNode *link;

};

class Chain {
    friend class ChainIterator;
	public :
		Chain(){first=0;}
	
	
		int Length() const;
		int Rearch(const int& x)const;
		Chain& Delete(int k,int& x);
		Chain& Insert(const int& x);
		Chain& LastInsert(int k,const int& x);
	
	private:
		ChainNode *first;



};
//链表遍历器类
class ChainIterator{

public:
	int* Initialize(const Chain &c){
	
		location =c.first;
		if(location) return &location->date;
		return 0;
	}
   int* Next(){
	
		if(!location) return 0;
		location=location->link;
		if(location) return &location->date;
		return 0;

	
	}
   
private:
	ChainNode *location;

};
//计算长度
int Chain::Length() const{

	ChainNode *current=new ChainNode;
	int len=0;
	while(current){
	
		len++;
		current=current->link;
	
	}

return len;
}
//插入
Chain &Chain::Insert(const int & x){

	ChainNode *y=new ChainNode;
	y->date=x;
	y->link=first;
	first=y;
	return *this;
}
//在末尾插入
Chain &Chain::LastInsert(int k,const int & x){
     ChainNode *p=first;
	for(int index=1;index<k&&p;index++)
		p=p->link;
	ChainNode *y=new ChainNode;
	y->date=x;
	if(k){
	
		y->link=p->link;
		p->link=y;
	}else{
	
		y->link=first;
		first=y;
	}

	 

return *this;



}
//删除
Chain &Chain::Delete(int k,int& x){

	if(k<1||!first)
	cout<<"不存在第"<<k<<"个元素,异常";
	ChainNode *p=first;
	if(k==1)
		first=first->link;
	else{
	
		ChainNode *q=first;
		for(int index=1;index<k-1&&q;index++)
			q=q->link;
		if(!q||!q->link)
			cout<<"不存在第"<<k<<"个元素,异常";
		p=q->link;
		q->link=p->link;
	}
x=p->date;
delete p;
return *this;

}
//搜索
int Chain::Rearch(const int& x)const{

	ChainNode *current=first;
	int index=1;
	while(current && current->date!=x){
	
		current=current->link;
		index++;
	
	}
if(current)
return index;
return 0;

}
//遍历器输出

void shuchu( Chain &X){
   	int *x;
	int s=0;
	ChainIterator c;
	x=c.Initialize(X);
	while(x){
        s++;
        x=c.Next();}
	
	x=c.Initialize(X);
    int	ss=0;
	while(x){
ss++;
if(ss<s)
    	cout<<*x<<",";
else
cout<<*x;
        x=c.Next();}
	cout<<endl;
	
}
//遍历器输出2
void shuchu2( Chain &X){
   	int *x;
	ChainIterator c;
	x=c.Initialize(X);
	while(x){

		cout<<*x<<",";
		x=c.Next();
	
	}

	
}
int main(){
	Chain s,s2;
    int num=0,num2=0;
	 cout<<"Input1"<<endl;
	int in;
	cin>>in;
	while(in!=0){
     s.LastInsert(num,in);
	num++;
	cin>>in;
	}

	 cout<<"Output1"<<endl;
	 shuchu(s);
	 cout<<"Input2"<<endl;
	 cin>>in;
	 s.Insert(in);
	 cout<<"Output2"<<endl;
	 shuchu(s);
	 cout<<"Input3"<<endl;
	 cin>>in;
	 int aa=s.Rearch(in);
	 cout<<"Output3"<<endl;
     cout<<aa<<endl;
	 cout<<"Input4"<<endl;
	 cin>>in;
	 aa=s.Rearch(in);
	 cout<<"Output4"<<endl;
     cout<<aa<<endl;
	 cout<<"Input5"<<endl;
	 int in2;
	cin>>in2;
	while(in2!=0){
     s2.LastInsert(num2,in2);
	num2++;
	cin>>in2;
	}

	 cout<<"Output5"<<endl;
	 shuchu(s2);
shuchu2(s);
shuchu(s2);
cout<<"End"<<endl;


return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值