Adjacent table template

Source: Internet
Author: User

Adjacent table template
LThe Processing Method of the adjacent table is as follows:LVertices in the graph are stored in a one-dimensional array. Of course, vertices can also be stored in a single-chain table. However, Arrays can easily read vertex information, making it easier.LIn the figure, all the adjacent points of each vertex Vi form a linear table. because the number of adjacent points is not sure, we choose to use a single-chain table for storage. 

 1 #include<iostream> 2 using namespace std; 3 struct node 4 { 5     int u; 6     int v; 7     int w; 8     int next; 9 }a[10001];10 int head[1001];11 int num=1;12 void f(int p,int b,int c)13 {14     a[num].u=p;15     a[num].v=b;16     a[num].w=c;17     a[num].next=head[p];18     head[p]=num++;19 }20 int main()21 {22     int n,m;23     cin>>n>>m;24     for(int i=1;i<=n;i++)25     head[i]=-1;26     for(int i=1;i<=m;i++)27     {28         int p,b,c;29         cin>>p>>b>>c;30         f(p,b,c);31     }32     33     cout<<"****************************"<<endl;34     for(int k=1;k<=n;k++)35     {36         int i=head[k];    37         cout<<"k:";38         while(i!=-1)39         {40             cout<<"-->"<<a[i].v;41             i=a[i].next;42         }43         cout<<"-->"<<-1<<" ";44         cout<<endl;45     }46 47     return 0;48 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.