forked from udulaindunil/Graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjList.cpp
90 lines (77 loc) · 1.38 KB
/
AdjList.cpp
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
#include<iostream>
#include<string>
using namespace std;
class graph{
private:
static const int graphsize = 4;
struct node{
string name;
char color;
node * next;
};
node *adjarr[graphsize];
string cities[4]={"Colombo","Galle","Matara","Gampaha"};
public:
graph();
void display();
void addedge(string f,string s);
};
graph::graph(){
for(int i=0;i<graphsize;i++){
adjarr[i] = new node;
adjarr[i]->name=cities[i];
adjarr[i]->color='W';
adjarr[i]->next=NULL;
}
}
void graph::addedge(string f,string s){
for(int i=0;i<graphsize;i++){
if(adjarr[i]->name==f){
node *ptr;
node *n;
ptr=adjarr[i];
while(ptr->next!=NULL){
ptr=ptr->next;
}
n=new node;
n->name=s;
n->color='W';
n->next=NULL;
ptr->next=n;
}
if(adjarr[i]->name==s){
node *ptr;
node *n;
ptr=adjarr[i];
while(ptr->next!=NULL){
ptr=ptr->next;
}
n=new node;
n->name=f;
n->color='W';
n->next=NULL;
ptr->next=n;
}
}
}
void graph::display(){
node *p;
for(int i=0;i<graphsize;i++){
p=adjarr[i];
while(p!=NULL){
cout<<p->name<<"\t\t";
p=p->next;
}
cout<<endl;
}
}
int main(){
cout<<"Adj Ary ||\t the link list nodes"<<endl;
cout<<"..................................."<<endl;
graph mm;
mm.addedge("Colombo","Gampaha");
mm.addedge("Colombo","Galle");
mm.addedge("Gampaha","Galle");
mm.display();
return 0;
}