zoukankan      html  css  js  c++  java
  • 图的遍历和二维矩阵动态分配内存

    #include<iostream.h>
    typedef struct node
    {
    	int order;
    	char data;
    	node *next;
    	bool tag;
    }*ptrn;
    typedef class linklist
    {
    protected:
    	int curposition;
    	ptrn curptr;
    public:
        ptrn head;
    	void sethead(ptrn &head1)
    	{
    		head=head1;
    		curposition=1;
    		curptr=head;
    	}
    	ptrn getelemptr(int position)
    	{
            if(curposition>position)
    		{
    			curposition=1;
    			curptr=head;
    		}
    		while(curposition<position)
    		{
    			curptr=curptr->next;
    			curposition++;
    		}
    		return curptr;
    		
    	}
    	int getcurposition()
    	{
    		return curposition;
    	}
    	int length()
    	{
    		ptrn temp;
    		temp=head;
    		int count=0;
    		while(1)
    		{
    			if(temp!=0)
    			{
    				count++;
    				temp=temp->next;
    			}
    			else
    				break;
    		}
    		return count;
    	}
    	
    	int getelem(int position)//获取链中第position位置的元素,其下标为n
    	{
    		int n;
    		if(position<1||position>length())
    		{
    			return -1;
    		}
    		else
    		{
    			ptrn tem;
    			tem=getelemptr(position);
    			n=tem->order;                          
    			return n;
    		}
    	}
    }*link;
    class linkqueue
    {
    private:
    	int maxsize;
        int *stack;
    	int top;
    	int count;
    public:
    	linkqueue(int size)
    	{
    		top=0;
    		count=0;
    		maxsize=size;
    		stack=new int[size];
    	}
    	void inqueue(int v)
    	{                                                  
    		if(count<=maxsize)
    		{
    			stack[top]=v;
    			count++;
    			top++;
    		}
    		else
    			cout<<"栈满"<<endl;
    	}
    	int outqueue()
    	{                                                   
    		if(count==0)
    		{
    			cout<<"栈为空"<<endl;
    			return -1;
    		}
    		else
    		{
    			int m;
                top--;
    			m=stack[top];                               
    			count--;
    			return m;
    		}
    	}
    	bool empty()
    	{
    		return count==0;
    	}
    };
    class adjmatrixdirgraph
    {
    protected:
    	char *elem;
    	int vexnum,edgenum;
    	int **matrix;
    	bool tag1;
    	ptrn *vextable;
    public:
        link *vexarray;
    	adjmatrixdirgraph(int size)
    	{
            
    		edgenum=0;
    		vexnum=size;
    		tag1=false;
            vexarray=new link[size];
    		vextable=new ptrn[size];
    		matrix=(int **)new int *[size];
    		elem=new char[vexnum];
    		cout<<"按顺序输入所有顶点的元素"<<endl;
            for(int i=0;i<vexnum;i++)
    		{
    			cin>>elem[i];
    		}
    		for(i=0;i<size;i++)
    			matrix[i]=new int[size];
    	}
    	void inputmatrix()
    	{
    		cout<<"请输入矩阵"<<endl;
    		for(int i=0;i<vexnum;i++)
    		{	
                cout<<"请输入第"<<i+1<<"行"<<endl;
    			for(int j=0;j<vexnum;j++)
    			{cin>>matrix[i][j];}
    		}
    		
    	}
    	void Matrix_to_adjlist()
    	{
    		ptrn temp;
           for(int i=0;i<vexnum;i++)
    	   {
    		   vextable[i]=new node;                           
    		   vextable[i]->data=elem[i];                      
    		   vextable[i]->order=i;  
    		   vextable[i]->tag=tag1;
    	   }
    	   for(i=0;i<vexnum;i++)
    	   {
    		   temp=vextable[i];                               
    		   for(int j=0;j<vexnum;j++)
    		   {
                    ptrn newnode;
    				newnode=new node;                          
    			   if(matrix[i][j]==0){}
    			   else
    			   {
                       newnode->order=vextable[j]->order;      
    				   newnode->data=vextable[j]->data;
    				   newnode->tag=vextable[j]->tag;
    				   temp->next=newnode;
    				   temp=temp->next;
    			   }
    		   }
    		   temp->next=0;
    	   }
    	}
    	void Adjlist_to_matrix()
    	{
    		ptrn temp;
    		for(int i=0;i<vexnum;i++)
    		{
    		    temp=vextable[i];
    			int j=0;
    			while(1)
    			{
    				if(temp==0)break;
    				else if(vextable[i]->order==j)
    				{
    					matrix[i][j]=0;
    					temp=temp->next;
    					j++; 
    				}
                    else if(temp->order==j)
    				{
    					matrix[i][j]=1;
    					temp=temp->next;
    					j++;
    				}
    				else
    				{
    					matrix[i][j]=0;
    					temp=temp->next;
    				}
    			}
    		}
    	}
    	void settag(int v,bool val)
    	{
    		if(v<0||v>=vexnum)
    			cout<<"越界1"<<endl;
    		else
    			vextable[v]->tag=val;
    	}
    	bool gettag(int v)
    	{
    		if(v<0||v>=vexnum)
    		{
    			cout<<"越界2"<<endl;
    			return true;
    		}
    		else
    			return vextable[v]->tag;
    	}
    	char getnode(int v)
    	{
    		char ch1;
             if(v<0||v>=vexnum)
    		 {
    			 cout<<"越界3"<<endl;
    			 return '*';
    		 }
    		else
    		{
    			ch1=vextable[v]->data;
    			return ch1;
    		}
    	}
        int indexhelp(link la,int v)//定位顶点v在链表中的位置
    	{
    		int curpos,adjv;
    		curpos=la->getcurposition();
    		adjv=la->getelem(curpos);
    		if(adjv==v)return curpos;
    		else
    		{
                curpos=1;
    		    for(curpos=1;curpos<=la->length();curpos++)
    			{                                                                
    			    adjv=la->getelem(curpos);
    			    if(adjv==v)break;
    			}                                                
    			return curpos;		
    		}
    		
    	}
    	int firstadjvex(int v)//返回顶点V的第一个邻接点
    	{
    		if(v<0||v>=vexnum)
    		{cout<<v<<"越界4"<<endl;}
    		else
    		{
    			link newnode=new linklist;                                 
    		    vexarray[v]=newnode;   
    		    ptrn e=vextable[v];
    		    vexarray[v]->sethead(e);  
    			                                                
    		    if(vexarray[v]->head==0)                                   
    			{return -1;}
    		    else
    			{
    			   int adjv;
                    adjv=vexarray[v]->getelem(2);
    			   return adjv;
    			   
    			}
    		}
    		
    	}
    	int nextadjvex(int v1,int v2)//返回顶点V1的相对于V2的下一个邻接点
    	{
    		if(v1<0||v1>=vexnum)cout<<"v1不合法"<<endl;
    		if(v2<0||v2>=vexnum)cout<<"v2不合法"<<endl;
    		if(v1==v2)cout<<"两者不能相等"<<endl;
    		else
    		{
    			link newnode=new linklist;                                 
    	       	vexarray[v1]=newnode; 
    		    ptrn e=vextable[v1];
    		    vexarray[v1]->sethead(e);
    		    if(vexarray[v1]->head==0)
    			{
    				return -1;
    			}
    			else
    			{
    				link list;
    				list=vexarray[v1];
    				 int cur;
    				 cur=indexhelp(list,v2);                                                                                                                           
    				 if(cur<list->length())
    				 {                                                    
                          
    			              int adjv;                                          
    		         	      adjv=list->getelem(cur+1);
    			              return adjv;
    				 }
    				 else
    				 {
    					  return -1;
    				 }
    		        
    			}
    		    
    		}
    	}
    	void DFS(int v)
    	{
            settag(v,true);
    		char ch2;
    		ch2=getnode(v);
    		cout<<ch2<<"  ";
    		for(int w=firstadjvex(v);w>=0;w=nextadjvex(v,w))
    		{
    			                                                         
    			if(!gettag(w))
    			{
    				DFS(w);
    			}
    			
    		}
    	}
    	void DFStravese()
    	{
    		int v;
    		for(v=0;v<vexnum;v++)
    		{
    			settag(v,false);
    		}
    		for(v=0;v<vexnum;v++)
    		{
    			if(!gettag(v))
    				DFS(v);
    		}
    		cout<<endl;
    	}
    	void BFS(int v)
    	{
    		settag(v,true);
    		char m;
    		m=getnode(v);
    		cout<<m<<"  ";
    		linkqueue q(vexnum);
    		q.inqueue(v);
    		while(!q.empty())
    		{
    			int u,w;
    			u=q.outqueue();                                 
    			for(w=firstadjvex(u);w>=0;w=nextadjvex(u,w))
    			{                                                  
    				if(!gettag(w))
    				{
    					settag(w,true);
    					char e;
    					e=getnode(w);
    					cout<<e<<"  ";
    					q.inqueue(w);
    				}
    			}
    	   
    		}
    	}
    	void BFStravese()
    	{
    		int v;
    		for(v=0;v<vexnum;v++)
    		{
    			settag(v,false);
    		}
    		for(v=0;v<vexnum;v++)
    		{
    			if(!gettag(v))
    				BFS(v);
    		}
    		cout<<endl;
    	}
    	void outputmatrix()
    	{
    		for(int i=0;i<vexnum;i++)
    		{
    			for(int j=0;j<vexnum;j++)
    			{cout<<matrix[i][j]<<"  ";}
    			cout<<endl;
    		}
    			
    	}
    	void outputadjlist()
    	{
    		for(int i=0;i<vexnum;i++)
    		{
    			ptrn temp;
    			temp=vextable[i];
    			while(1)
    			{
                    if(temp!=0)
    				{
    					cout<<temp->order<<temp->data<<"  ";
    					temp=temp->next;
    				}
    				else
    					break;
    			}
    			cout<<endl;
    		}
    	}
    };
    void main()
    {
        int vexnumber;
    	cout<<"请输入顶点的数目"<<endl;
    	cin>>vexnumber;
        adjmatrixdirgraph adj(vexnumber);
        adj.inputmatrix();
    	adj.outputmatrix();
    	adj.Matrix_to_adjlist();
    	adj.outputadjlist();
    	adj.Adjlist_to_matrix();
    	adj.outputmatrix();
    	cout<<"图的深度优先遍历顺序为"<<endl;
    	adj.DFStravese();
    	cout<<"图的广度优先遍历顺序为"<<endl;
    	adj.BFStravese();
    }
    

  • 相关阅读:
    001-docker-net-网络设置分类、Bridge详解、mac docker说明
    010-Linux 磁盘信息查看
    011-Spring aop 002-核心说明-切点PointCut、通知Advice、切面Advisor
    【高并发】Akka 模型
    【大数据】Spark On Yarn
    【Git】git pull和git pull --rebase的使用
    【Zookeeper】连接ZooKeeper的方式
    【Cloud】IaaS、PaaS和SaaS
    【Linux】解决"no member named 'max_align_t'
    【神经网络】Dependency Parsing的两种解决方案
  • 原文地址:https://www.cnblogs.com/zztong/p/6695305.html
Copyright © 2011-2022 走看看