https://blog.csdn.net/nyist_yangguang/article/details/113468345
The adjacency list is actually very simple to implement with a linked list, but the program is relatively long.
If you change your thinking and use arrays to implement adjacency lists, this method is easy to understand, intuitive, and simple to operate.
test program
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int u[100],v[100],w[100];
int first[100],next[100];
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1; i<=m; i++)
{
scanf("%d %d %d",&u[i],&v[i],&w[i]);
first[i]=-1;
next[i]=-1;
}
for(int i=1; i<=m; i++)
{
if(first[u[i]]==-1)
first[u[i]]=i;
else
{
next[i]=first[u[i]];
first[u[i]]=i;
}
}
int temp;
for(int i=1; i<=m; i++)
{
temp=first[i];
while(temp!=-1)
{
cout<<u[temp]<<" "<<v[temp]<<" "<<w[temp]<<endl;
temp=next[temp];
}
}
}
Test Results