Untitled
unknown
plain_text
3 years ago
5.9 kB
8
Indexable
#include <iostream>
using namespace std;
const int n = 20;
struct link {
int key;
link* next;
} *gr[n];
void init(link* gr[n]) {
for (int i = 0; i < n; i++) {
gr[i] = NULL;
}
}
int search_node(link* gr[n], int c) {
int flag = 0;
for (int i = 0; i < n; i++) {
if (gr[i]) {
if (gr[i]->key == c) {
flag = 1;
break;
}
}
}
return flag;
}
int search_arc(link* gr[n], int c1, int c2) {
int flag = 0;
if (search_node(gr, c1) && search_node(gr, c2)) {
int i = 0;
while (gr[i]->key != c1 || gr[i] == NULL)
i++;
link* p = gr[i];
while (p->key != c2 && p->next != NULL)
p = p->next;
if (p->key == c2)
flag = 1;
}
return flag;
}
void add_node(link* gr[n], int c) {
if (search_node(gr, c)) {
cout << endl << "Existing node!" << endl;
}
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 << endl << "Overflow!" << endl;
}
}
}
void add_arc(link* gr[n], int c1, int c2) {
int i = 0;
link* p;
if (search_arc(gr, c1, c2)) {
cout << endl << "Existing arc!" << endl;
}
else {
if (!(search_node(gr, c1)))
add_node(gr, c1);
if (!(search_node(gr, c2)))
add_node(gr, c2);
while (gr[i]->key != c1 || gr[i] == NULL)
i++;
p = new link;
p->key = c2;
p->next = gr[i]->next;
gr[i]->next = p;
}
}
void del_node(link* gr[n], int c) {
if (search_node(gr, c)) {
int i = 0;
while (gr[i]->key != c)
i++;
link* p, * q = NULL;
while (gr[i] != NULL) {
p = gr[i];
gr[i] = p->next;
delete p;
}
for (int i = 0; i < n; i++) {
if (gr[i]) {
p = gr[i];
while ((p->key != c) && (p->next != NULL)) {
q = p;
p = p->next;
}
if (p->key == c) {
q->next = p->next;
delete p;
}
}
}
}
else {
cout << "The node is not in the graph!" << endl;
}
}
void del_arc(link* gr[n], int c1, int c2) {
if (search_arc(gr, c1, c2)) {
int i = 0;
while (gr[i]->key != c1 || gr[i] == NULL)
i++;
link* p = gr[i], * q = NULL;
while (p->key != c2) {
q = p;
p = p->next;
}
q->next = p->next;
delete p;
}
else {
cout << "The arc is not in the graph!" << endl;
}
}
void print_node(link* gr[n]) {
for (int i = 0; i < n; i++) {
if (gr[i]) {
cout << gr[i]->key;
link* p = gr[i]->next;
while (p) {
cout << "->" << p->key << endl;
p = p->next;
}
}
}
cout << endl;
system("pause");
}
void visualizeAndDeleteLoops(link* gr[n]) {
cout << endl << "Graph Visualization:" << endl;
print_node(gr);
cout << endl;
// Detect and delete loops
for (int i = 0; i < n; i++) {
if (gr[i]) {
link* p = gr[i]->next;
link* prev = gr[i];
while (p) {
if (p->key == gr[i]->key) {
// Delete the loop
prev->next = p->next;
delete p;
p = prev->next;
}
else {
p = p->next;
prev = prev->next;
}
}
}
}
cout << "Graph after deleting loops:" << endl;
print_node(gr);
cout << endl;
}
int main() {
int x, y, choice;
do {
system("cls");
cout << endl << "MENU" << endl;
cout << "1. Initialize graph." << endl;
cout << "2. Add node." << endl;
cout << "3. Add arc." << endl;
cout << "4. Delete node." << endl;
cout << "5. Delete arc." << endl;
cout << "6. Print graph." << endl;
cout << "7. Visualize and delete loops." << endl;
cout << "8. Exit." << endl;
cout << "Choose an option: ";
cin >> choice;
switch (choice) {
case 1:
init(gr);
break;
case 2:
cout << endl << "Enter a node to add: ";
cin >> x;
add_node(gr, x);
break;
case 3:
cout << endl << "Enter the origin and destination of the arc to add: ";
cin >> x >> y;
add_arc(gr, x, y);
break;
case 4:
cout << endl << "Enter a node to delete: ";
cin >> x;
del_node(gr, x);
break;
case 5:
cout << endl << "Enter the origin and destination of the arc to delete: ";
cin >> x >> y;
del_arc(gr, x, y);
break;
case 6:
cout << endl << "Graph:" << endl;
print_node(gr);
cout << endl;
break;
case 7:
visualizeAndDeleteLoops(gr);
break;
case 8:
cout << endl << "Exiting the program." << endl;
break;
default:
cout << endl << "Invalid option! Please try again." << endl;
break;
}
} while (choice != 8);
return 0;
}
Editor is loading...