温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

链表的可变参数构造与图的临接表实现   广度有限遍历

发布时间:2020-07-23 14:30:40 来源:网络 阅读:320 作者:wzdouban 栏目:编程语言
 
#include <bits/stdc++.h>
using namespace std;
typedef  struct node
{
  int x; 
  node*next;
  node(){next=NULL;}
}node;
typedef struct head
{
    int x;
    int count;
    node*head;
    //head(int xx,int cc,node* P=NULL){x=xx;count=cc;head=p;}
}head;

void InsertTail(node *head,int val)
{
	if(head == NULL)
		return ;
	node *tmp = (node *)malloc(sizeof(node)*1);
	tmp->next = NULL;
	tmp->x = val;
	while(head->next != NULL)
	{
		head = head->next;
	}
	head->next = tmp;
}



/* VS2008*/
void List(node *head,...)
{
	if(head == NULL) return ;
	char* p = (char *)&head+4;
	int n = *(int *)p;
	int x = 0;
	for(int i=0;i < n;++i)
	{
		p = p+sizeof(int);
		x = *(int *)p;
		InsertTail(head,x);
	}
	
}
void show(node*head)
{
  node *p = head->next;    
    while(p != NULL)
    {
        cout<<p->x<<" ";
        p = p->next;
    }
    cout<<endl;
}
int  test()
{
    head   H[11];
    node *head0  = (node *)malloc(sizeof(node));
    node *head1  = (node *)malloc(sizeof(node));
    node *head2  = (node *)malloc(sizeof(node));
    node *head3  = (node *)malloc(sizeof(node));
    node *head4  = (node *)malloc(sizeof(node));
    node *head5  = (node *)malloc(sizeof(node));
    node *head6  = (node *)malloc(sizeof(node));
    node *head7  = (node *)malloc(sizeof(node));
    node *head8  = (node *)malloc(sizeof(node));
    node *head9  = (node *)malloc(sizeof(node));
   node *head10  = (node *)malloc(sizeof(node));
    List(head0,1,1);         H[0].head=head0;   
    List(head1,2,2,10);      H[1].head=head1;    
    List(head3,2,2,4);       H[3].head=head3;    
    List(head5,2,4,6);       H[5].head=head5;    
    List(head7,2,6,8);       H[7].head=head7;    
    List(head9,2,8,10);      H[9].head=head9;    
    List(head2,4,2,3,4,10);  H[2].head=head2;    
    List(head4,4,2,3,5,6);   H[4].head=head4;    
    List(head6,4,4,5,7,8);   H[6].head=head6;     
    List(head8,4,6,7,9,10);  H[8].head=head8;    
    List(head10,4,1,2,8,9); H[10].head=head10;
     for(int i=1;i<=10;i++)
    {
     H[i].x=i;H[i].count=(i%2)?2:4;     
    } 
   // show(head1);show(head2);show(head3);show(head4);show(head5);
  //  show(head6);show(head7);show(head8);show(head9);show(head10);
   
  queue<head>q;
  vector<int>v;
   q.push(H[0]);
  while(!q.empty())
    {
   head h=q.front();
      node*p=h.head->next;
      while(p!=NULL)
       {
          if(find(v.begin(),v.end(),p->x)==v.end())
          {
            cout<<p->x<<"   ";
            v.push_back(p->x);
            q.push(H[p->x]);
          }          
          p=p->next;
       } 
    q.pop();    
    }
 cout<<endl;
  
	return 0;
}
int main()
{
    test();
    
	cout << "Hello,C++ world of AnycodeX!" << endl;
	return 0;
}

五角星  

特别说明

这里的代码  anycodes编译不过

需要设计一个通用的可变参,初始化链表;

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI