Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- class Node{
- public:
- int destination;
- int weight;
- Node *Next ;
- };
- class graphsion
- {
- public:
- Node *head=NULL;
- };
- void AddEdge(graphsion arr[],int src,int dest,int w)
- {
- Node *newnode = new Node();
- newnode->destination = dest;
- newnode->weight= w;
- newnode->Next = NULL;
- if(arr[src].head == NULL)
- arr[src].head = newnode;
- else{
- Node *temp =arr[src].head;
- temp =arr[w].head;
- while(temp->Next != NULL)
- temp = temp->Next;
- temp->Next = newnode;
- }
- }
- void display(graphsion listt[],int v)
- {
- int i;
- for(i=0;i<v;i++)
- {
- cout<<"\n adjacency list of vertex-"<<i<<"->";
- while(listt[i].head != NULL)
- {
- cout<<" "<<listt[i].head->destination<<"->"<<listt[i].head->weight;
- listt[i].head = listt[i].head->Next;
- }
- }
- }
- int main()
- {
- freopen("input.txt","r",stdin);
- int i,v;
- cin>>v;
- graphsion a[v];
- for( i=0;i<=v;i++)
- a[i].head=NULL;
- int m,n,w;
- cin>>m>>n>>w;
- while(scanf("%d%d",&m,&n,&w) == 3)
- {
- AddEdge(a,m,n,w);
- AddEdge(a,n,m,w);
- }
- display(a,v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement