⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.cpp

📁 为了读取二叉树的结点数据
💻 CPP
字号:
#include<iostream.h>
#define maxnode 8
#define max     5
struct node 
{
	int mark;
	node *next;
};
struct Graph
{
	int n,e;
	node *graphvertex[maxnode];
};
Graph*creatgraph()
{
	Graph*graph=new Graph;
	if(graph==NULL)
	{
		cout<<"memory is not available"<<endl;

		return NULL;
	}

	cout<<"please put in the node of the graph "<<endl;
	cin>>graph->n;
	cout<<"please put in the edge of the graph"<<endl;
	cin>>graph->e;
	for(int i=0;i<graph->n;i++)
	{
		node*tp=new node;
		tp->mark=i;
		tp->next=NULL;
		graph->graphvertex[i]=tp;
	}
	for(int j=0;j<graph->e;j++)
	{
		node*temp=new node;
		if(temp==NULL)
		{
			cout<<"no memory available"<<endl;
			return NULL;
		}
	temp->mark=0;
	temp->next=NULL;
	int a,b;
	cout<<"please put in the edge from a to b "<<endl;
	cin>>a>>b;
	temp->mark=b;
	temp->next=graph->graphvertex[a]->next;
	graph->graphvertex[a]->next=temp;
	node*s=new node;
		if(s==NULL)
		{
			cout<<"no memory available"<<endl;
			return NULL;
		}
	s->mark=0;
	s->next=NULL;
	s->mark=a;
	s->next=graph->graphvertex[b]->next;
	graph->graphvertex[b]->next=s;
	}
	return graph;
}
class Queue
{
private:
	int i,j,count;
	int b[max];
public:
	Queue()
	{
		i=0;
		j=0;
		count=0;
		for(int m=0;m<max;m++)
			b[m]=0;
	}
	int outqueue()
	{
		if(count>0)
		{
			
			i=++i%max;
		    count--;
			return b[(i-1+max)%max];
		}
	}
	inqueue(int c)
	{

		if(count<max)
		{
			b[j]=c;
			j=++j%max;
			count++;
		}
		else
			cout<<"the memory is not available because of the queue"<<endl;
	}
};
int num=0;
int a[maxnode]={0};
travel(Graph *graph,Queue queue)
{
	while(num<graph->n)
		{
			int i=0;
	     i=queue.outqueue();
	    if(a[i]==0)//not necessery
		{
			cout<<i<<"  ";
		a[i]=1;
		num++;
		}
     node *p=graph->graphvertex[i]->next;
	while(p)
	{
		if(a[p->mark]==0)
			queue.inqueue(p->mark);
		p=p->next;
	}
	}
}
main()
	{
		Graph *graph;
		graph=creatgraph();
		Queue queue;
        queue.inqueue(graph->graphvertex[0]->mark);
		travel(graph,queue);
	}








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -