数据结构编程问题(三)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

继续

一、问题

1.链表
2.暴力法
3.查找数据
4.索引表
5.矩阵乘法
6.求取最大值
7.容器相关问题
8.符号重载
9.平衡树
10.子列

二、代码

1-5

代码如下:

1.#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
struct node
{
	int data;
	node*next;
};
node*Insert(node*head,node*p)
{
	node*p1,*p2=NULL;
	if(head==NULL)
	{
		head=p;
		p->next=NULL;
		return(head);
	}
	p1=head;
	while((p->data)>(p1->data)&&p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if((p->data)<=(p1->data))
	{
		p->next=p1;
		if(head==p1) head=p;
		else p2->next=p;
	}
	else
	{
		p1->next=p;
		p->next=NULL;
	}
	return(head);
}
node*sortcreate(void)
{
	node*p,*head=NULL;
	srand(time(0));
	int a=rand();
	int m;
	cout<<"请您输入希望链表的长度:"<<endl;
	cin>>m;
	while(m>0)
	{
		p=new node;
		p->data=a;
		head=Insert(head,p);
		a=rand();
		m--;
	}
	return(head);
}
node*addup(node*La,node*Lb)
{
	node*Lc=((La->data)<=(Lb->data))?La:Lb;
	node*pc=Lc;
	node*pa=La->next;
	node*pb=Lb;
	node*p;
	while(pa&&pb)
	{
		if(pa->data<=pb->data)
		{
			if(pa->data<pb->data)
			{
				pc->next=pa;
				pc=pa;
				pa=pa->next;
			}
			else
			{
				pc->next=pa;
				pc=pa;
				pa=pa->next;
				p=pb;
				pb=pb->next;
				delete p;
			}
		}
		else
		{
			pc->next=pb;
			pc=pb;
			pb=pb->next;
		}
	}
		pc->next=pa?pa:pb;
		return(Lc);
}
void free(node*head)
{
	node*p;
	while(head)
	{
		p=head;
		head=head->next;
		delete p;
	}
}
void show(node*head)
{
	int i=0;
	node*p=head;
	while(p)
	{
		if(i%5==0) cout<<'\n';
		cout<<p->data<<'\t';
		i++;
		p=p->next;
	}
	cout<<endl;
}
int main()
{
	node*La,*Lb,*Lc;
	La=sortcreate();
	Lb=sortcreate();
	cout<<"La数据为:";
	show(La);
	cout<<"Lb数据为:";
	show(Lb);
	Lc=addup(La,Lb);
	cout<<"Lc数据为:";
	show(Lc);
	free(Lc);
	return 0;
}
2.#include<iostream>
using namespace std;
#define N 50 
int index(char S[N],char T[N],int pos)
{
	int i=pos,j=0;
	int m=strlen(S),n=strlen(T);
	while(i<m&&j<n)
	{
		if(S[i]==T[j]){++i;++j;}
		else{i=i-j+2;j=1;}
	}
	if(j>=n)
		return i-n;
	else return 0;
}
int main()
{
	char S[N]={"xbcdabcdabxabasyu"},T[N]={"bcd"};
	int pos=0;
	pos=index(S,T,pos);
	cout<<"位次是:"<<pos<<endl;
	return 0;
}
3.#include<iostream>
using namespace std;
int findelem(int p[],int n)
{
	int low,mid,high;
	high=p[0];
	low=1;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(p[mid]==n) return mid;
		if(p[mid]>n)
		{
			high=mid-1;
			continue;
		}
		if(p[mid]<n)
		{
			low=mid+1;
			continue;
		}
	}
	return 0;
}
int main()
{
	int p[20];
	int m;
	cout<<"请输入数据个数:";
	cin>>m;
	p[0]=m;
	int i;
	cout<<"请输入数据:";
	for(i=1;i<=m;i++)
		cin>>p[i];
	cout<<"请输入想要查找的数据:";
	cin>>m;
	i=findelem(p,m);
	if(i==0)
		cout<<"没有所查找的数据。"<<endl;
	else
		cout<<"查找的数据下标为:"<<i<<endl;
	return 0;
}
4.#include<iostream>
using namespace std;
struct excel
{
	int max;
	int address;
};
void produce(int p[],int k,excel*&ST)
{
	int n=p[0]/k;
	ST=new excel[n+2];
	int i,j;
	int m;
	for(i=1;i<=n;i++)
	{
		m=p[k*(i-1)+1];
		if(i==n)
		{
			for(j=k*(i-1)+1;j<=p[0];j++)
				if(m<p[j]) m=p[j];
		}
		else
		{
			for(j=k*(i-1)+1;j<=i*k;j++)
				if(m<p[j]) m=p[j];
		}
		ST[i].max=m;
		ST[i].address=k*(i-1)+1;
	}
	ST[0].max=n;
	ST[i].max=-1;
}
int findelem(excel*ST,int p[],int n)
{
	int i,j;
	int r=ST[0].max;
	for(i=1;i<=r;i++)
	{
		if(ST[i].max>=n)
			break;
	}
	if(i>r)
		return 0;
	if(ST[i+1].max!=-1)
	{
		int t=ST[i+1].address;
		for(j=ST[i].address;j<t;j++)
			if(p[j]==n) return j;
		return 0;
	}
	else
	{
		for(j=ST[i].address;j<=p[0];j++)
			if(p[j]==n) return j;
		return 0;
	}
}
int main()
{
	excel*ST;
	int p[50];
	int n,k;
	cout<<"请输入数据个数:";
	cin>>n;
	p[0]=n;
	cout<<'\n'<<"请输入希望的索引表步长:";
	cin>>k;
	int i;
	cout<<"请按照指定格式输入数据:";
	for(i=1;i<=n;i++)
		cin>>p[i];
	produce(p,k,ST);
	do
	{
		cout<<"请输入想要查找的数据:";
		cin>>n;
		i=findelem(ST,p,n);
		if(i==0)
			cout<<"不存在这样的数据。"<<endl;
		else
			cout<<"您查找的数据所在位数为:"<<i<<endl;
		cout<<"请您输入‘1’继续或者输入‘0’结束。"<<endl;
		cin>>i;
	}while(i);
	return 0;
}
5.#include<iostream>
using namespace std;
#define MAX 100
struct trimple
{
	int i,j;
	int e;
};
struct jz
{
	trimple data[MAX+1];
	int rpos[MAX+1];
	int mu,nu,tu;
};
void RPOS(jz&T)
{
	int num[MAX+1];
	int row;
	if(T.tu)
	{
		for(row=1;row<=T.mu;++row)
			num[row]=0;
		for(int t=1;t<=T.tu;++t)
			++num[T.data[t].i];
		T.rpos[1]=1;
		for(row=2;row<=T.mu;++row)
			T.rpos[row]=T.rpos[row-1]+num[row-1];
	}
}
bool cheng(jz M,jz N,jz&Q)
{
 	if(M.nu!=N.mu) return false;
	Q.mu=M.mu;
	Q.nu=N.nu;
	Q.tu=0;
	int ctemp[MAX+1]={0};
	int tp,brow,ccol,t,arow;
	if(M.tu*N.tu!=0)
	{
		for(arow=1;arow<=M.mu;++arow)
		{
			for(ccol=1;ccol<=Q.nu;++ccol)
				ctemp[ccol]=0;
			Q.rpos[arow]=Q.tu+1;
			if(arow<M.mu) tp=M.rpos[arow+1];
			else tp=M.tu+1;
			for(int p=M.rpos[arow];p<tp;++p)
			{
				brow=M.data[p].j;
				if(brow<N.mu) t=N.rpos[brow+1];
				else t=N.tu+1;
				for(int q=N.rpos[brow];q<t;++q)
				{
					ccol=N.data[q].j;
					ctemp[ccol]+=M.data[p].e*N.data[q].e;
				}
			}
			for(ccol=1;ccol<=Q.nu;++ccol)
			{
				if(ctemp[ccol])
				{
					if(++Q.tu>MAX) return false;
				    Q.data[Q.tu].e=ctemp[ccol];
				    Q.data[Q.tu].i=arow;
				    Q.data[Q.tu].j=ccol;
				}
			}
		}
	}
	return true;
}
void output(jz T)
{
	if(T.tu)
	{
		int t=1;
		for(int row=1;row<T.mu+1;row++)
		{
			for(int col=1;col<T.nu+1;col++)
			{
				if(T.data[t].i==row&&T.data[t].j==col)
				{
					cout<<T.data[t].e<<'\t';
					t++;
				}
				else cout<<0<<'\t';
			}
			cout<<'\n';
		}
	}
}
int main()
{
	jz M,N,Q;
	int t;
	M.tu=4;
	N.tu=4;
	M.mu=3;
	M.nu=4;
	N.mu=4;
	N.nu=2;
	for(t=1;t<M.tu+1;t++)
		cin>>M.data[t].e>>M.data[t].i>>M.data[t].j;
	for(t=1;t<N.tu+1;t++)
		cin>>N.data[t].e>>N.data[t].i>>N.data[t].j;
	cout<<"原始矩阵为:"<<endl;
	output(M);
	output(N);
	RPOS(M);
	RPOS(N);
	if(cheng(M,N,Q))
	{
		cout<<"结果矩阵为:"<<endl;
		output(Q);
	}
	else cout<<"数据有误!"<<endl;
	return 0;
}


6-10

代码如下:

6.#include<iostream>
using namespace std;

int main()
{
	int max=0;
	int a[4]={1,2,3,4};
	for(int i=0;i<4;i++)
	{
		if(max<a[i])
			max=a[i];
		cout<<a[i];
	}
	return 0;
}
7.#include<iostream>
using namespace std;
template <typename T>
class CValue
{
	T a[5];
public:
	CValue(T b[5])
	{
		for(int i=0;i<5;i++)
			a[i]=b[i];
	}
	friend T max(CValue<T> m);
	friend T min(CValue<T> m);
};
T max(CValue<T> m)
{
	T max=m.a[0];
	for(int i=0;i<5;i++)
	{
		if(max<m.a[i]) max=m.a[i];
	}
	return max;
}
T min(CValue<T> m)
{
	T min=m.a[0];
	for(int i=0;i<5;i++)
	{
		if(min>m.a[i]) min=m.a[i];
	}
	return min;
}
int main()
{
	int a1[5]={0},i=0;
	float a2[5]={0};
	double a3[5]={0};
	for(i=0;i<5;i++)
		cin>>a1[i];
	for(i=0;i<5;i++)
		cin>>a2[i];
	for(i=0;i<5;i++)
		cin>>a3[i];
	CValue<int>c1(a1);
	CValue<float>c2(a2);
	CValue<double>c3(a3);
	cout<<max(c1)<<'\t'<<min(c1)<<'\n'<<max(c2)<<'\t'<<min(c2)<<'\n'<<max(c3)<<'\t'<<min(c3)<<endl;
	return 0;
}
8.#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<memory>
#include<cstdlib>
using namespace std;
struct Review
{
	string title;
	int rating;
	int price;
};
bool operator<(const shared_ptr<Review>&r1,const shared_ptr<Review>&r2)
{
	if(r1->title<r2->title)
		return true;
	else if(r1->title==r2->title&&r1->rating<r2->rating)
		return true;
	else
		return false;
}
bool worseThan(const shared_ptr<Review>&r1,const shared_ptr<Review>&r2)
{
	if(r1->rating<r2->rating)
		return true;
	else
		return false;
}
bool betterThan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
	if (r1->rating > r2->rating)
		return true;
	else
		return false;
}
bool worseThanP(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
	if (r1->price < r2->price)
		return true;
	else
		return false;
}
bool betterThanP(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
	if (r1->price > r2->price)
		return true;
	else
		return false;
}
bool FillReview(shared_ptr<Review>&rr)
{
	cout<<"Enter book title(quit to quit):";
	getline(cin,rr.title);
	if(rr->title=="quit")
		return false;
	cout<<"Enter book rating:";
	cin>>rr->rating;
	if(!cin)
		return false;
	while(cin.get()!='\n')
		continue;
	return true;
}
void ShowReview(const shared_ptr<Review>&rr)
{
	cout<<rr->rating<<"\t"<<rr->title<<'\t'<<rr->price<<endl;
}
shared_ptr<Review> make_Review()
{
	return shared_ptr<Review>(new Review);
}
int main()
{
	vector<shared_ptr<Review>>books;
	shared_ptr<Review> temp(new Review);
	while(FillReview(temp))
	{
		books.push_back(temp);
        temp=make_Review();	
	}
	if(books.size()>0)
	{
		vector<shared_ptr<Review>> sbook(books);
		cout << "Thank you. You entered the following:\n"<< books.size() << " ratings:\n" << "Rating\tBook\tPrice\n";
		for_each(books.begin(), books.end(), ShowReview);
		char ch;
		cout << "Enter measures of sort:\no to old";
		cout << "t to title, r to down rating,\nR to up rating"<<"p to down price,P to up price, f(F) to shuffle,\nq to quit:";
		cin >> ch;
		while (tolower(ch) != 'q')
		{
			switch (ch)
			{
			case'o':
				cout << "Not Sort:\nRating\tBook\tPrice\n";
				for_each(books.begin(), books.end(), ShowReview);
				break;
			case't':
				sort(sbook.begin(), sbook.end());
				cout << "Sorted by title:\nRating\tBook\tPrice\n";
				for_each(sbook.begin(), sbook.end(), ShowReview);
				break;
			case'r':
				sort(sbook.begin(), sbook.end(), worseThan);
				cout << "Sorted by  down rating:\nRating\tBook\tPrice\n";
				for_each(sbook.begin(), sbook.end(), ShowReview);
				break;
			case'R':
				sort(sbook.begin(), sbook.end(), betterThan);
				cout << "Sorted by  up rating:\nRating\tBook\tPrice\n";
				for_each(sbook.begin(), sbook.end(), ShowReview);
				break;
			case'p':
				sort(sbook.begin(), sbook.end(), worseThanP);
				cout << "Sorted by down price:\nRating\tBook\tPrice\n";
				for_each(sbook.begin(), sbook.end(), ShowReview);
				break;
			case'P':
				sort(sbook.begin(), sbook.end(), betterThanP);
				cout << "Sorted by up price:\nRating\tBook\tPrice\n";
				for_each(sbook.begin(), sbook.end(), ShowReview);
				break;
			case'F':
			case'f':
				random_shuffle(sbook.begin(), sbook.end());
				cout << "After shuffling:\nRating\tBook\n";
				for_each(sbook.begin(), sbook.end(), ShowReview);
				break;
			default:
				cout << "Error input!Input again!";
				break;
			}
			cout << "Enter the next measures:\no to old";
			cout << "t to title, r to down rating,\nR to up rating"
				"p to down price,P to up price, q to quit:";
			cin >> ch;	
		}
	}
	else
		cout << "No entries. ";
	cout<<"Bye.\n";
	return 0;
}
9.#include<iostream>
using namespace std;
#define LH 1
#define EH 0
#define RH -1
struct sorttree
{
	int data;
	int be;
	sorttree*left,*right;
};
void rightturn(sorttree*&p)
{
	sorttree*lc=p->left;
	p->left=lc->right;
	lc->right=p;
	p=lc;
}
void leftturn(sorttree*&p)
{
	sorttree*rc=p->right;
	p->right=rc->left;
	rc->left=p;
	p=rc;
}
void isequal(int e,sorttree*T,bool&ifequal)
{
	if(T)
	{
		isequal(e,T->left,ifequal);
		if(T->data==e)
			ifequal=true;
		isequal(e,T->right,ifequal);
	}
}
void leftbalance(sorttree*&T)
{
	sorttree*lc=T->left;
	sorttree*rd;
	switch(lc->be)
	{
	case LH:
		T->be=lc->be=EH;
		rightturn(T);break;
	case RH:
		rd=lc->right;
		switch(rd->be)
		{
		case LH:T->be=RH;lc->be=EH;break;
		case EH:T->be=lc->be=EH;break;
		case RH:T->be=EH;lc->be=LH;break;
		}
		rd->be=EH;
		leftturn(T->left);
		rightturn(T);
	}
}
void rightbalance(sorttree*&T)
{
	sorttree*rd=T->right;
	sorttree*lc;
	switch(rd->be)
	{
	case RH:
		T->be=rd->be=EH;
		leftturn(T);break;
	case LH:
		lc=rd->left;
		switch(lc->be)
		{
		case LH:T->be=EH;rd->be=RH;break;
		case EH:T->be=rd->be=EH;break;
		case RH:T->be=LH;rd->be=EH;break;
		}
		lc->be=EH;
		rightturn(T->right);
		leftturn(T);
	}
}
int insert(sorttree*&T,int e,bool&higher)
{
	if(!T)
	{
		T=new sorttree;
		T->data=e;
		T->left=T->right=NULL;
		T->be=EH;
		higher=true;
	}
	else
	{
		bool ifequal=false;
		isequal(e,T,ifequal);
		if(ifequal)
		{
			higher=false;
			return 0;
		}
		ifequal=false;
		if(e>T->data)
		{
			isequal(e,T->right,ifequal);
		    if(!ifequal)
			{
				if(!insert(T->right,e,higher)) return 0;
			    if(higher)
				{
					switch(T->be)
					{
					case LH:T->be=EH;higher=false;break;
				    case EH:T->be=RH;higher=true;break;
				    case RH:rightbalance(T);higher=false;break;
					}
				}
			}
			else
			{
				if(!insert(T,e,higher)) return 0;
				if(higher)
				{
					switch(T->be)
					{
					case LH:leftbalance(T);higher=false;break;
				    case EH:T->be=LH;higher=true;break;
				    case RH:T->be=EH;higher=false;break;
					}
				}
			}
		}
		else
		{
			isequal(e,T->left,ifequal);
			if(!ifequal)
			{
				if(!insert(T->left,e,higher)) return 0;
				if(higher)
				{
					switch(T->be)
					{
					case LH:leftbalance(T);higher=false;break;
				    case EH:T->be=LH;higher=true;break;
				    case RH:T->be=EH;higher=false;break;
					}
				}
			}
			else
			{
				if(!insert(T,e,higher)) return 0;
			    if(higher)
				{
					switch(T->be)
					{
					case LH:T->be=EH;higher=false;break;
				    case EH:T->be=RH;higher=true;break;
				    case RH:rightbalance(T);higher=false;break;
					}
				} 
			}
		}
	}
	return 1;
}
void visit(sorttree*root)
{
	cout<<root->data<<'\t'<<root->be<<endl;
}
void inorder(sorttree*T)
{
	if(T)
	{
		inorder(T->left);
		visit(T);
		inorder(T->right);
	}
}
void shanchu(sorttree*T)
{
	if(T)
	{
		shanchu(T->left);
		shanchu(T->right);
		delete T;
	}
}
int main()
{
	sorttree*T=NULL;
	int i,n,k;
	bool higher;
	cout<<"请您输入数据个数:";
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>k;
		insert(T,k,higher);
	}
	inorder(T);
	shanchu(T);
	return 0;
}
10.#include<iostream>
using namespace std;
#define N 50 
int index(char S[N],char T[N],int pos)
{
	int i=pos,j=0;
	int m=strlen(S),n=strlen(T);
	while(i<m&&j<n)
	{
		if(S[i]==T[j]){++i;++j;}
		else{i=i-j+2;j=1;}
	}
	if(j>=n)
		return i-n;
	else return 0;
}
int main()
{
	char S[N]={"xbcdabcdabxabasyu"},T[N]={"bcd"};
	int pos=0;
	pos=index(S,T,pos);
	cout<<"位次是:"<<pos<<endl;
	return 0;
}

总结

敬请期待第四篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值