溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

鏈表的可變參數(shù)構(gòu)造與圖的臨接表實現(xiàn)   廣度有限遍歷

發(fā)布時間:2020-07-23 14:30:40 來源:網(wǎng)絡(luò) 閱讀:322 作者: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編譯不過

需要設(shè)計一個通用的可變參,初始化鏈表;

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI