-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassg5_Adj_list
115 lines (87 loc) · 1.68 KB
/
assg5_Adj_list
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Problem statement :
// There are flight paths between cities. If there is a flight between city A and city B then there is an edge between the cities. The cost of the edge can be the time that flight take to reach city B from A, or the amount of fuel used for the journey. Represent this as a graph. The node can be represented by airport name or name of the city.
//Use adjacency list representation of the graph or use adjacency matrix representation of the graph.
// Check whether the graph is connected or not. Justify the storage representation used.
#include<iostream>
using namespace std;
struct node
{
string data;
int fuel;
node *next;
};
class graph1
{
public:
node * Head[10];
int v,e;
void create();
void display();
void edge();
};
void graph1::create()
{
cout<<"How many Vertices : ";
cin>>v;
cout<<"Enter Name of Cities : \n ";
for(int i=0;i<v;i++)
{
Head[i]=new node;
cin>>Head[i]->data;
Head[i]->next=NULL;
}
}
void graph1::display()
{
node *n2;
for(int i=0;i<v;i++)
{
cout<<Head[i]->data<<"--->";
n2=Head[i]->next;
while(n2!=NULL)
{
cout<<n2->data<<"|"<<n2->fuel<<"->";
n2=n2->next;
}
cout<<endl;
}
}
void graph1::edge()
{
int i,j;
string s,d;
node *n1 ,*temp;
cout<<"How many Edges : ";
cin>>e;
for( i=0;i<e;i++)
{
cout<<"Enter Source n Dest :";
cin>>s>>d;
for(j=0;j<v;j++)
{
if(s==Head[j]->data)
{
break;
}
}
n1=new node;
n1->data=d;
cout<<"\n Enter Fuel Required : ";
cin>>n1->fuel;
n1->next=NULL;
temp=Head[j];
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=n1;
}
}
int main()
{
graph1 g1;
g1.create();
g1.edge();
g1.display();
return 0;
}