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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <iostream>
using namespace std;
const int n = 5;
struct link
{
char key;
link *next;
} *gr[n];
void init(link *gr[n]);
void add_node(link *gr[n], char c);
void add_arc(link *gr[n], char c1, char c2);
int search_node(link *gr[n], char c);
int search_arc(link *gr[n], char c1, char c2);
void list_node(link *gr[n]);
void init(link *gr[n])
{
for (int i = 0; i < n; i++)
gr[i] = NULL;
}
void add_node(link *gr[n], char c)
{
if (search_node(gr, c))
cout << "\n Existing node!";
else
{
int j = 0;
while (gr[j] && (j < n))
j++;
if (gr[j] == NULL)
{
gr[j] = new link;
gr[j]->key = c;
gr[j]->next = NULL;
}
else
cout << "\n Overflow!";
}
}
void add_arc(link *gr[n], char c1, char c2)
{
int i = 0;
link *p;
if (search_arc(gr, c1, c2))
{
cout << "\n Existing arc!";
}
else
{
if (!(search_node(gr, c1)))
add_node(gr, c1);
if (!(search_node(gr, c2)))
add_node(gr, c2);
while (gr[i]->key != c1)
i++;
p = new link;
p->key = c2;
p->next = gr[i]->next;
gr[i]->next = p;
}
}
int search_node(link *gr[n], char c)
{
int flag = 0;
for (int i = 0; i < n; i++)
{
if (gr[i])
if (gr[i]->key == c)
flag = 1;
}
return flag;
}
int search_arc(link *gr[n], char c1, char c2)
{
int flag = 0;
if (search_node(gr, c1) && search_node(gr, c2))
{
int i = 0;
while (gr[i]->key != c1) i++;
link *p = gr[i];
while (p->key != c2 && p->next != NULL)
p = p->next;
if (p->key == c2)
flag = 1;
}
return flag;
}
void list_node(link *gr[n])
{
cout << "\n ";
for (int i = 0; i < n; i++)
{
if (gr[n])
cout << gr[i]->key;
}
}
int main()
{
char c, k;
int m;
do
{
cout << "\n Main menu \n";
cout << "\n 1 - Init";
cout << "\n 2 - Add node";
cout << "\n 3 - Add arc";
cout << "\n 4 - Search node";
cout << "\n 5 - Search arc";
cout << "\n 6 - Print all";
cout << "\n 7 - Exit";
cout << "\n Your choice --> ";
cin >> m;
switch (m)
{
case 1:
{
init(gr);
break;
}
case 2:
{
for (int i = 0; i < n; i++)
{
cout << "\n Enter node(char): ";
cin >> c;
add_node(gr, c);
}
break;
}
case 3:
{
for (int i = 0; i < 5; i++)
{
cout << "\n Enter first node (char): ";
cin >> c;
cout << "\n Enter second node (char): ";
cin >> k;
add_arc(gr, c, k);
}
break;
}
case 4:
{
cout << "\n Enter node (char): ";
cin >> k;
if (search_node(gr, k))
cout << "\n Search result: Yes!";
else
cout << "\n Search result: NO!";
break;
}
case 5:
{
cout << "\n Enter first node (char): ";
cin >> c;
cout << "\n Enter second node (char): ";
cin >> k;
if (search_arc(gr, c, k))
cout << "\n Search result: Yes!";
else
cout << "\n Search result: NO!";
break;
}
case 6:
{
list_node(gr);
}
} // end of switch
} while (m != 7);
cout << "\n";
system("pause");
}
|