-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathgraphadj.cpp
More file actions
92 lines (73 loc) · 1.7 KB
/
graphadj.cpp
File metadata and controls
92 lines (73 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*==============================|-problem statement-|==============================*/
/*
Represent a graph using adjacency list.
*/
/*===================================|-solution-|===================================*/
#include <iostream>
#include <stdlib.h>
using namespace std;
/* linked list node*/
struct node{
public:
int vert;
int weight;
node* nxt;
node(int vert, int weight){
this -> vert = vert;
this -> weight = weight;
this -> nxt = nullptr;
}
node(int vert, int weight,node* top){
this -> vert = vert;
this -> weight = weight;
this -> nxt = top;
}
};
/* linked list*/
struct LL{
public:
node* top;
LL(){
top = nullptr;
}
void add(int vert,int weight){
this->top = new node(vert, weight, top);
}
void display(){
node *temp = top;
while(temp != nullptr){
cout<<temp->vert<<"->";
temp = temp->nxt;
}
}
};
int main(){
int V ;
cout<<"Number of vertices in the graph \n";
cin>>V;
LL *list[V];//graph //pointer array of linked lists
int start, end, weight;
//intilaiseng the graph
for(int i =0;i<V;i++){
list[i] = new LL();
}
cout<<"enter edges . to quit press -1\n";
//takes inputs stops when given -1
while(1){
cin>>start;
if(start == -1){
break;
}
cin>>end>>weight;
list[start]->add(end, weight);
list[end]->add(start, weight);
}
cout<<"the graph is: \n";
//displays
for(int i =0;i<V;i++){
cout<<i<<"->";
list[i]->display();
cout<<"null \n";
}
return 0;
}