Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int n = 6;
- struct graf {
- char key;
- graf *next;
- } *gr[n];
- void init(graf *gr[n]) {
- for (int i = 0; i < n; i++)
- gr[i] = nullptr;
- }
- int search_node(char c, graf *gr[n]) {
- 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(graf *gr[n], char c_1, char c_2) //tyrsene na dyga (ot vryh do vryh)
- {
- int flag = 0;
- if (search_node(c_1, gr) && search_node(c_2, gr)) {
- int i = 0;
- while (gr[i] == nullptr || gr[i]->key != c_1)
- i++;
- graf *p = gr[i];
- while (p->key != c_2 && p->next)
- p = p->next;
- if (p->key == c_2)
- flag = 1;
- }
- return flag;
- }
- void add_node(char c, graf *gr[n]) { //dobavqne na vyzel
- if (search_node(c, gr))
- cout << "\nNqma takyv vyzel!!\n";
- else {
- int i = 0;
- while (gr[i] && i < n)
- i++;
- if (gr[i] == nullptr) {
- gr[i] = new graf;
- gr[i]->key = c;
- gr[i]->next = nullptr;
- }
- else
- cout << "\nPrepylnen graf !!" << endl;
- }
- }
- void add_arc(char c1, char c2, graf *gr[n]) { //dobavqne na dyga
- int i = 0;
- graf *p;
- if (search_arc(gr, c2, c1))
- cout << "\nDygata syshtestvuva !!" << endl;
- else {
- if (!search_node(c1, gr))
- add_node(c1, gr);
- if (!search_node(c2, gr))
- add_node(c2, gr);
- if (search_node(c1, gr) && search_node(c2, gr)) {
- int i = 0;
- while (gr[i] == nullptr || gr[i]->key != c1)
- i++;
- graf *p = new graf;
- p->key = c2;
- p->next = gr[i]->next;
- gr[i]->next = p;
- }
- else
- cout << "\nDygata ne e v grafa !!" << endl;
- }
- }
- void del_node(char c, graf *gr[n]) {
- if (search_node(c, gr)) { //iztrivane na izhodqshtite dygi
- int i = 0;
- while (gr[i] == nullptr || gr[i]->key != c)
- i++;
- graf *p = gr[i];
- while (gr[i]) {
- p = gr[i];
- gr[i] = gr[i]->next;
- delete p;
- }
- for (i = 0; i < n; i++) { //iztrivane na vhodqshti dygi
- graf *p = gr[i], *q = p;
- while (p->key != c && p->next) {
- q = p;
- p = p->next;
- }
- if (p->key == c) {
- q->next = p->next;
- delete p;
- }
- }
- }
- }
- void del_arc(char c1, char c2, graf *gr[n]) {
- if (search_arc(gr, c1, c2)) {
- int i = 0;
- while (gr[i] == nullptr || gr[i]->key != c1)
- i++;
- graf *p = gr[i], *q = p;
- while (p->key != c2) {
- q = p;
- p = p->next;
- }
- q->next = p->next;
- delete p;
- }
- }
- void list_gr(graf *gr[n]) {
- for (int i = 0; i < n; i++) {
- if (gr[i]) {
- graf *p = gr[i];
- while (p) {
- cout << p->key << "\t" << endl;
- p = p->next;
- }
- cout << endl;
- }
- }
- }
- void isolated(graf *gr[n]) {
- int br[n] = { 0 };
- int broqch = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (gr[i] != gr[j]) {
- if (search_arc(gr, gr[i]->key, gr[j]->key) || search_arc(gr, gr[j]->key, gr[i]->key)) { // proverqva vhodqshtata dyga i izhodqshtata dyga
- br[i]++; // broi na vsichki syshtestvvashti dygi
- //cout << gr[i]->key << "->" << gr[j]->key << "dygata systhtestvyva\n";
- }
- }
- }
- if (!br[i]) {// ako broqcha e 0
- cout << gr[i]->key << "vyrhyt e izoliran" << endl;
- broqch++;
- }
- }
- cout << "\nBroi izolirani vyrhove:" << broqch << endl;
- }
- int main() {
- init(gr);
- add_arc('A', 'B', gr);
- add_arc('A', 'C', gr);
- add_arc('A', 'D', gr);
- add_arc('C', 'D', gr);
- add_node('E', gr);
- add_node('F', gr);
- isolated(gr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement