-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphimp.cpp
More file actions
120 lines (94 loc) · 1.58 KB
/
Copy pathgraphimp.cpp
File metadata and controls
120 lines (94 loc) · 1.58 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
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
#include <iostream>
using namespace std;
class list
{
public:
int destination ;
list * next ;
list();
};
list :: list()
{
next = NULL;
}
class Graph
{
public:
int index ;
list * ptr ;
Graph * next ;
Graph();
}*head = NULL;
Graph :: Graph()
{
ptr = NULL;
next = NULL;
}
void create_node ( int orig )
{
Graph * temp = new Graph ;
temp->index = orig;
if (head == NULL)
{
head=temp;
}
else
{
Graph * start = head ;
while(start->next != NULL)
start = start -> next ;
start->next = temp ;
}
}
void create_link (int origin , int destination)
{
Graph * temp = head ;
while (temp != NULL)
{
if(temp->index ==origin)
{
list * node = new list ;
node->destination = destination;
if(temp->ptr==NULL)
temp->ptr=node;
else
{
list * temp1=temp->ptr;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=node;
}
}
temp=temp->next;
}
}
void display ()
{
Graph * temp = head ;
while (temp != NULL)
{
cout<<temp -> index ;
list * trav = temp -> ptr ;
while (trav != NULL)
{
cout << " -> " <<trav -> destination;
trav=trav->next;
}
cout<<endl;
temp = temp -> next ;
}
}
int main()
{
create_node (0);
create_node (1);
create_node (2);
create_node (3);
create_link (0,1);
create_link (0,2);
create_link (0,3);
create_link (2,3);
create_link (3,2);
create_link (2,1);
display();
}