图的矩阵与邻接矩阵转换和遍历

#include
typedef struct node
{
	int order;
	char data;
	node *next;
	bool tag;
}*ptrn;
class linklist
{
protected:
	int curposition;
	ptrn curptr;
	int count;
public:
    ptrn head;
	void sethead(ptrn &head1)
	{
		head=head1;
		curposition=0;
		curptr=head;
		count=0;
	}
	ptrn getelemptr(int position)
	{
        if(curposition>position)
		{
			curposition=0;
			curptr=head;
		}
		for(;curpositionnext;
		return curptr;
	}
	int getcurposition()
	{
		return curposition;
	}
	int length()
	{
		ptrn temp;
		temp=head;
		while(1)
		{
			if(temp!=0)
			{
				count++;
				temp=temp->next;
			}
			else
				break;
		}
		return count;
	}
	
	int getelem(int position,int n)//获取链中第position位置的元素,其下标为n
	{
		if(position<1||position>length()+1)
			cout<<"越界"<order;
			return n;
		}
	}
};
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<<"栈满"<>elem[i];
		}
		for(i=0;i>matrix[i][j];
	}
	void Matrix_to_adjlist()
	{
		ptrn temp;
       for(int i=0;idata=elem[i];                      
		   vextable[i]->order=i;  
		   vextable[i]->tag=tag1;
	   }
	   for(i=0;iorder=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;iorder==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<<"越界"<tag=val;
	}
	bool gettag(int v)
	{
		if(v<0||v>=vexnum)
			cout<<"越界"<tag;
	}
	void getnode(int v,char ch)
	{
         if(v<0||v>=vexnum)
			cout<<"越界"<data;
	}
    int indexhelp(linklist *la,int v)//定位顶点v在链表中的位置
	{
		int curpos,adjv;
		curpos=la->getcurposition();
		adjv=la->getelem(curpos,adjv);
		if(adjv==v)return curpos;
		else
		{
            curpos=1;
		    for(curpos=1;curpos<=la->length();curpos++)
			{
			    adjv=la->getelem(curpos,adjv);
			    if(adjv==v)break;
			}
			return curpos;		
		}
		
	}
	int firstadjvex(int v)//返回顶点V的第一个邻接点
	{
		if(v<0||v>=vexnum)
		{cout<<"越界"<adjlink=new linklist;  
		    ptrn e=vextable[v];
			vexarray[v]->ch=e->data;
		    vexarray[v]->adjlink->sethead(e);                         
		    if(vexarray[v]->adjlink->head==0)                                   
			{return -1;}
		    else
			{
			   int adjv;
                adjv=vexarray[v]->adjlink->getelem(1,adjv);
			   return adjv;
			   
			}
		}
		
	}
	int nextadjvex(int v1,int v2)//返回顶点V1的相对于V2的下一个邻接点
	{
		if(v1<0||v1>=vexnum)cout<<"v1不合法"<=vexnum)cout<<"v2不合法"<adjlink=new linklist; 
		    ptrn e=vextable[v1];
		    vexarray[v1]->adjlink->sethead(e);
		    if(vexarray[v1]->adjlink->head==0)
			{
				cout<<"没有以"<adjlink,v2);
				 if(curadjlink->length())
				 {
                      
			              int adjv;
		         	      adjv=vexarray[v1]->adjlink->getelem(cur+1,adjv);
			              return adjv;
				 }
				 else
				 {
                      cout<=0;w=nextadjvex(v,w))
		{
			if(!gettag(w))
				DFS(w);
		}
	}
	void DFStravese()
	{
		int v;
		for(v=0;v=0;w=nextadjvex(u,w))
			{
				if(!gettag(w))
				{
					settag(w,true);
					char e;
					getnode(w,e);
					cout<order<data<<"  ";
					temp=temp->next;
				}
				else
					break;
			}
			cout<

你可能感兴趣的:(图的矩阵与邻接矩阵转换和遍历)