数据结构的相关编程问题

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


前言

本文是一些之前对数据结构的问题编程


一、设计到的问题

1.矩阵的转置
2.矩阵的乘法
3.简单的生成树
4.中序遍历和后序遍历
5.前序遍历生成树
6.线索化二叉树
7.索引顺序表的查找
8.折半查找
9.平衡树
10.图的深度优先遍历

二、相关代码

1-5

代码如下:

1.#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
	srand(time(0));
	int n=rand()%21,i,j;
	while(n<5)
		n=rand()%21;
	int**A;
	A=new int*[n];
	for(i=0;i<n;i++)
		A[i]=new int[n];
	cout<<"该三对角阵行数与列数为:"<<n<<'\n'<<"请您输入数据:";
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(j>i-2&&j<i+2)
				cin>>A[i][j];
			else A[i][j]=rand();
		}
	}
	cout<<"原矩阵为:"<<endl;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			cout<<A[i][j]<<'\t';
		}
		cout<<endl;
	}
	int*b=new int[3*20-2];
	int k=0;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{	
			if(j>i-2&&j<i+2)
			{
				b[k]=A[i][j];
				k++;
			}
		}
	}
	cout<<"请您输入想要查找的数据行数与列数:";
	cin>>i>>j;
	cout<<'\n'<<"数据为:"<<b[i*2+j]<<endl;
	int **B;
	B=new int*[n];
	for(i=0;i<n;i++)
		B[i]=new int[n];
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(j>i-2&&j<i+2)
				B[i][j]=b[2*i+j];
			else B[i][j]=0;
		}
	}
	cout<<"还原矩阵为:"<<endl;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			cout<<B[i][j]<<'\t';
		}
		cout<<endl;
	}
	delete []b;
	for(i=0;i<n;i++)
	{
		delete []A[i];
		delete []B[i];
	}
	delete []A;
	delete []B;
	return 0;
}
2.#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;
}
3.#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void produce(int*p,int n)
{
	srand(time(0));
	int i=1,m=rand()%25;
	while(i<=n)
	{
		p[i]=m;
		i++;
		m=rand()%25;
	}
}
void output(int*p,int n)
{
	for(int i=1;i<=n;i++)
		cout<<p[i]<<endl;
}
int father(int k,int n)
{
	int j;
	if(k==1) return 0;
	if(k<=n)
	{
		j=k/2;
		return j;
	}
	if(k>n) return -1;
}
int rightchild(int k,int n)
{
	int j=2*k+1;
	if(j>n||j<=1)
		return 0;
	if(j<=n)
		return j;
}
int main()
{
	srand(time(0));
	int n=rand()%50;
	cout<<n<<endl;
	int*p=new int[n+1];
	produce(p,n);
	int k,j;
	cout<<"请输入下标值:";
	cin>>k;
	j=father(k,n);
	if(j==0) cout<<"No parents!"<<endl;
	else 
	{
		if(j<0) cout<<"输入数据有误!"<<endl;
		else cout<<"Its father is:"<<' '<<j<<endl;
	}
	j=rightchild(k,n);
	if(j) cout<<"Its rightchild is: "<<j<<endl;
	else cout<<"输入数据有误!"<<endl;
	output(p,n);
	delete []p;
	return 0;
}
4.#include<iostream>
using namespace std;
struct tree
{
	char data;
	tree*left;
	tree*right;
};
void visit(tree*root)
{
	cout<<root->data;
}
void produce(tree*&T)
{
	char ch;
	cin>>ch;
	T=new tree;
	if(ch=='@') T=NULL;
	else
	{
		T->data=ch;
		produce(T->left);
		produce(T->right);
	}
}
void inorder(tree*T)
{
	if(T)
	{
		inorder(T->left);
		visit(T);
		inorder(T->right);
	}
}
void postorder(tree*T)
{
	if(T)
	{
		postorder(T->left);
		postorder(T->right);
		visit(T);
	}
}
int Find(char*array,int size,char v)
{
	for(int i=0;i<size;i++)
	{
		if(array[i]==v)
		{
			return i;
		}
	}
	return -1;
}
tree*inproduce(char*postorder,char inorder[],int size)
{
	if(size<=0) return NULL;
	int rootvalue=postorder[size-1];
	int r=Find(inorder,size,rootvalue);
	tree*head=new tree;
	head->data=postorder[size-1];
	head->left=inproduce(postorder,inorder,r);
	head->right=inproduce(postorder+r,inorder+r+1,size-1-r);
	return head;
}
int main()
{
	tree*root=NULL;
	tree*head=NULL;
	cout<<"请您输入数据:";
	produce(root);
	cout<<"该树为:";
	inorder(root);
	cout<<endl;
	postorder(root);
	cout<<endl;
	cout<<"请输入数据数目:";
	int size;
	cin>>size;
	char*arry=new char[size+1];
	char*post=new char[size+1];
	cout<<"请您输入中序排列:";
	for(int i=0;i<size;i++)
		cin>>arry[i];
	cout<<"请您输入后续排列:";
	for(int j=0;j<size;j++)
		cin>>post[j];
	head=inproduce(post,arry,size);
	inorder(head);
	cout<<endl;
	postorder(head);
	cout<<endl;
	delete []arry;
	delete []post;
	return 0;//ABC@@DE@G@@F@@@
5.#include<iostream>
#include<stack>
using namespace std;
enum TAG {L,R};
struct tree
{
	char data;
	tree*left;
	tree*right;
};
struct wtree
{
	tree*ptr;
	TAG tag;
};
void visit(tree*root)
{
	cout<<root->data;
}
void produce(tree*&T)
{
	char ch;
	cin>>ch;
	T=new tree;
	if(ch=='@') T=NULL;
	else
	{
		T->data=ch;
		produce(T->left);
		produce(T->right);
	}
}
void postorder(tree*T)
{
	stack<wtree> S;
	wtree w;
	tree*p=T;
	do
	{
		while(p!=NULL)
		{
			w.ptr=p;
			w.tag=L;
			S.push(w);
			p=p->left;
		}
		int flag=1;
		while(flag&&!S.empty())
		{
			w=S.top();
			S.pop();
			p=w.ptr;
			switch(w.tag)
			{
			case L:w.tag=R;S.push(w);flag=0;p=p->right;break;
			case R:visit(p);break;
			}
		}
	}while(!S.empty());
}
int main()
{
	tree*root=NULL;
	cout<<"请您输入数据:";
	produce(root);
	cout<<"该树为:";
	postorder(root);
	cout<<endl;
	return 0;//ABC@@DE@G@@F@@@
}

6-10

代码如下:

6.#include<iostream>
using namespace std;
struct tree
{
	char data;
	tree*left;
	tree*right;
	bool ltag;
	bool rtag;
};
void visit(tree*root)
{
	cout<<root->data;
}
void produce(tree*&T)
{
	char ch;
	cin>>ch;
	T=new tree;
	if(ch=='@') T=NULL;
	else
	{
		T->data=ch;
		T->ltag=false;
		T->rtag=false;
		produce(T->left);
		produce(T->right);
	}
}
void inorder(tree*T)
{
	if(T)
	{
		inorder(T->left);
		visit(T);
		inorder(T->right);
	}
}
void createIn(tree*current,tree*&pre)
{
	if(current==NULL)
		return;
	createIn(current->left,pre);
	if(current->left==NULL)
	{
		current->left=pre;current->ltag=true;
	}
	if(pre!=NULL&&pre->right==NULL)
	{
		pre->right=current;
		pre->rtag=true;
	}
	pre=current;
	createIn(current->right,pre);
}
void createIn(tree*root)
{
	tree*pre=NULL;
	if(root!=NULL)
	{
		createIn(root,pre);
		pre->right=NULL;
		pre->rtag=true;
	}
}
tree* front(tree*current)
{
	tree*p=current;
	while(!(p->ltag))
		p=p->left;
	return p;
}
tree* next(tree*current)
{
	tree*p=current;
	if(p->rtag)
		return p->right;
	else
	{
		p=p->right;
		return front(p);
	}
}
void inordersearch(tree*T)
{
	tree*p;
	for(p=front(T);p!=NULL;p=next(p))
		visit(p);
}
int main()
{
	tree*root=NULL;
	cout<<"请您输入数据:";
	produce(root);
	cout<<"该树为:";
	inorder(root);
	cout<<endl;
	createIn(root);
	cout<<"利用线索化二叉树的方式输出:";
	inordersearch(root);
	cout<<endl;
	return 0;//ABC@@DE@G@@F@@@
}
7.#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;
}
8.#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;
}
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>
#include<stack>
#include<cstdlib>
#include<ctime>
using namespace std;
struct Arcnode
{
	int j;
	Arcnode*next;
	int i;
};
struct Vnode
{
	int elem;
	int data;
	Arcnode*first;
};
struct Graph
{

	Vnode*v;
	int length;
};
void depthorder(Graph g)
{
	int n=g.length;
	int*visit=new int[n];
	int i;
	for(i=0;i<n;i++)
		visit[i]=0;
	stack<Vnode> s;
	Vnode*v=g.v;
	Vnode e;
	Arcnode*a;
	s.push(v[0]);
	visit[0]=1;
	while(!s.empty())
	{
		e=s.top();
		s.pop();
		cout<<e.data<<'\t';
		a=e.first;
		while(a!=NULL)
		{
			i=a->j;
			if(visit[i]==0)
			{
				s.push(v[i]);
				visit[i]=1;
			}
			a=a->next;
		}
	}
	delete []visit;
}		
void adjlist(int*p,int a[][20],int n,Vnode*v)
{
	int i,j;
	Arcnode*r,*c;
	bool flag=true;
	for(i=0;i<n;i++)
	{
		v[i].elem=i;
		v[i].data=p[i];
		v[i].first=NULL;
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(a[i][j]==1&&flag)
			{
				r=new Arcnode;
				r->i=i;
				r->j=j;
				r->next=NULL;
				v[i].first=r;
				flag=false;
			}
			else
			{
				if(a[i][j]==1&&!flag)
				{
					r=new Arcnode;
					r->i=i;
					r->j=j;
					r->next=NULL;
					c=v[i].first;
					while(c->next!=NULL)
						c=c->next;
					c->next=r;
				}
			}
		}
		flag=true;
	}
}
void matrix(int*p,int a[][20],int n)
{
	srand(time(0));
	int i,j;
	for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
			a[i][j]=rand()%2;
	for(i=0;i<n;i++)
		for(j=0;j<i;j++)
			a[i][j]=a[j][i];
	for(i=0;i<n;i++)
		a[i][i]=0;
}//随机生成一个图
void vipmatrix(int*p,int a[][20],int n)
{
	srand(time(0));
	int i,j;
	bool flag;
	for(i=0;i<n;i++)
	{
		flag=false;
		for(j=i+1;j<n;j++)
		{
			a[i][j]=rand()%2;
			if(a[i][j]==1) flag=true;
		}
		if(!flag)
			a[i][n-1]=1;
	}	
	for(i=0;i<n;i++)
		for(j=0;j<i;j++)
			a[i][j]=a[j][i];
	for(i=0;i<n;i++)
		a[i][i]=0;
}//生成一颗连通图
int main()
{
	srand(time(0));
	int n=rand()%20;
	int *p=new int[n];
	int i,j;
	for(i=0;i<n;i++)
		p[i]=rand()%200;
	cout<<"该图的元素为:";
	for(i=0;i<n;i++)
		cout<<p[i]<<'\t';
	cout<<endl;
	int a[20][20];
	matrix(p,a,n);//这是随机生成一个图,如果想要生成连通图,就改为:vipmatrix(p,a,n);
	cout<<"该图的邻接矩阵为:"<<endl;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			cout<<a[i][j]<<'\t';
		cout<<endl;
	}
	Vnode*v=new Vnode[n];
	adjlist(p,a,n,v);
	cout<<"链接表为:"<<endl;
	Arcnode*r,*c;
	for(i=0;i<n;i++)
	{
		cout<<i<<":"<<v[i].data<<'\t';
		r=v[i].first;
		if(r)
		{
			while(r->next!=NULL)
			{
				cout<<r->j<<"->";
				r=r->next;
			}
		    cout<<r->j<<endl;
		}
		else
			cout<<endl;
	}
	Graph g;
	g.v=v;
	g.length=n;
	cout<<"该图的深度优先遍历为:";
	depthorder(g);
	delete []p;
	for(i=0;i<n;i++)
	{
		r=v[i].first;
		while(r)
		{
			c=r;
			r=r->next;
			delete c;
		}
	}
	delete []v;
	return 0;
}

总结

本期十个问题:
1.矩阵的转置
2.矩阵的乘法
3.简单的生成树
4.中序遍历和后序遍历
5.前序遍历生成树
6.线索化二叉树
7.索引顺序表的查找
8.折半查找
9.平衡树
10.图的深度优先遍历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值