-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.txt
142 lines (122 loc) · 3.68 KB
/
05.txt
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//Write a function to get the number of vertices in an undirected graph and its edges. You may
assume that no edge is input twice.
i. Use adjacency list representation of the graph and find runtime of the function
ii. Use adjacency matrix representation of the graph and find runtime of the function
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{ char vertex;
node *next;
};
class adjmatlist
{ int m[10][10],n,i,j; char ch; char v[20]; node *head[20]; node *temp=NULL;
public:
adjmatlist()
{ for(i=0;i<20;i++)
{ head[i]=NULL; }
}
void getgraph();
void adjlist();
void displaym();
void displaya();
};
void adjmatlist::getgraph()
{
cout<<"\n enter no. of vertices in graph(max. 20)";
cin>>n;
cout<<"\n enter name of vertices";
for(i=0;i<n;i++)
cin>>v[i];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ cout<<"\n edge present between "<<v[i]<<"-----"<<v[j]<<" then press enter y otherwise n";
cin>>ch;
if(ch=='y')
{ m[i][j]=1; }
else if(ch=='n')
{ m[i][j]=0; }
else
{ cout<<"\n unknown entry"; }
}
}
adjlist();
}
void adjmatlist::adjlist()
{
for(i=0;i<n;i++)
{ node *p=new(struct node);
p->next=NULL;
p->vertex=v[i];
head[i]=p; // cout<<"\n"<<head[i]->vertex;
}
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{
if(m[i][j]==1)
{
node *p=new(struct node);
p->vertex=v[j];
p->next=NULL;
if(head[i]->next==NULL)
{ head[i]->next=p; }
else
{ temp=head[i];
while(temp->next!=NULL)
{ temp=temp->next; }
temp->next=p;
}
}
}
}
}
void adjmatlist::displaym()
{
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
cout<<m[i][j]<<" ";
cout<<"\n";
}
}
void adjmatlist::displaya()
{
cout<<"\n adjacency list is";
for(i=0;i<n;i++)
{
if(head[i]==NULL)
{ cout<<"\n adjacency list not present"; break; }
else
{
cout<<"\n"<<head[i]->vertex;
temp=head[i]->next;
while(temp!=NULL)
{ cout<<"-> "<<temp->vertex;
temp=temp->next; }
}
}
}
int main()
{ int m;
adjmatlist a;
while(1)
{
cout<<"\n\n enter the choice";
cout<<"\n 1.enter graph";
cout<<"\n 2.display adjacency matrix";
cout<<"\n 3.display adjacency list";
cout<<"\n 4.exit";
cin>>m;
switch(m)
{ case 1: a.getgraph();
break;
case 2: a.displaym();
break;
case 3: a.displaya();
break;
case 4: exit(0);
default: cout<<"\n unknown choice";
}
}
return 0;
}