Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Node{
- public:
- int destination;
- int weight;
- Node *next;
- };
- class Integer{
- public:
- Node *head = NULL;
- };
- void addEdge(Integer arr[],int sorc,int dest)
- {
- Node *newNode = new Node();
- newNode->destination = dest;
- newNode->next = NULL;
- if( arr[sorc].head == NULL)
- arr[sorc].head = newNode;
- else
- {
- Node *temp = arr[sorc].head;
- while(temp->next != NULL)
- temp = temp->next;
- temp->next = newNode;
- }
- }
- void display(Integer listt[], int v)
- {
- for(int 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 = listt[i].head->next;
- }
- }
- }
- int main()
- {
- freopen("input.txt","r",stdin);
- int v;
- cin>>v;
- Integer arr[v];
- /// eita na korle arr er vitore v+1 dite hobe
- for (int i = 0; i <= v; i++)
- arr[i].head = NULL;
- /// getting input
- int m,n;
- while(scanf("%d%d",&m,&n) == 2)
- {
- addEdge(arr,m,n);
- addEdge(arr,n,m);
- }
- display(arr,v);
- }
- ///test case:
- /*
- 7
- 1 2
- 1 3
- 2 3
- 4 5
- 5 6
- 5 3
- 6 2
- 6 4
- 3 7
- 5 7
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement