graph problem

Hello everyone.I have to write a program which returns the min(max) key values ​​of the peak for a given graph.So far I have been able to writte the part for adding and searching for a node and arc.My idea is to use a Depth-first search and use a min/max function but failed to come up with working salutation.
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
  #include<iostream>
using namespace std;

const int n=10;
struct elem
{
	char key;
	elem * next;
}*g[n];


int convert(char k, elem *g[n]);
void init(elem * gr[n]);
int search_node(elem * g[], char c);
int search_arc(elem * g[], char c1, char c2);
void add_node(elem * g[n], char c);
void add_arc(elem * g[n], char c1, char c2);
void print(elem * g[n]);


int main()
{
	char c,d;
	int m, vhod, izhod;

	do{
		cout<<"\n\tMenu\n";
		cout<<"1-init\n";
		cout<<"2-add node\n";
		cout<<"3-add arc\n";
		cout<<"4-search node\n";
		cout<<"5-search arc\n";
                cout<<"6-print\n";
                 cout<<"7-exit\n";

		cin>>m;

		switch(m)
		{
		case 1: { init(g); break; }
		case 2: { cout<<"\nInput c: "; cin>>c; add_node(g,c); break; }
		case 3: { cout<<"\nInput first node: "; cin>>c; cout<<"\nInput second node: "; cin>>d; add_arc(g,c,d) ; break; }
		case 4: { cout<<"\nInput node for searching: "; cin>>c;
			if(search_node(g,c)) cout<<"Yep ;)";
			else cout<<"Nope :(";
			break; }
		case 5: { cout<<"\nInput nodes for arc:\nc= "; cin>>c; cout<<"\nd= "; cin>>d;
			if(search_arc(g,c,d)) cout<<"Yep ;)";
			else cout<<"Nope :(";
			break; }

		case 6: { print(g) ; break; }


		case 7: { break; }
		}
	}while(m!=10);

}


void init(elem * gr[n])
{
	for(int i=0;i<n;i++)
		g[i]=NULL;
}


int search_node(elem * g[], char c)
{
	bool flag=false;
	for(int i=0;i<n;i++)
		if(g[i])
			if(g[i]->key==c)
				flag=true;
	return flag;
}


int search_arc(elem * g[], char c1, char c2)
{
	elem * t;
	bool flag=false;

	if(!search_node(g,c1))
		return false;
	if(!search_node(g,c2))
		return false;

	for(int i=0;i<n;i++)
	{
		if(g[i])
			if(g[i]->key==c1)
			{
				t=g[i];
				while(t->next)
				{
				t=t->next;
				if(t->key==c2) flag=true;
				}
			}
	}

	return flag;
}


void add_node(elem * g[n], char c)
{
	if(search_node(g,c))
		cout<<"\Existing node!\n";
	else
	{
		int j=0;
		while(g[j]&&j<n)	j++;
		if(g[j]==NULL)
		{
			g[j]=new elem;
			g[j]->key=c;
			g[j]->next=NULL;
		}
		else
			cout<<"\nOverflow!\n";
	}
}


void add_arc(elem * g[n], char c1, char c2)
{
	int i=0;
	elem * p;
	if(search_arc(g,c1,c2))
		cout<<"\nExisting arc!";
	else
	{
		if(!(search_node(g,c1)))
			add_node(g,c1);
		if(!(search_node(g,c2)))
			add_node(g,c2);
		while(g[i]->key!=c1)	i++;
		p=new elem;
		p->key=c2;
		p->next=g[i]->next;
		g[i]->next=p;
	}
}




void print(elem * g[n])
{
	elem * t;
	for(int i=0;i<n;i++)
	{
		if(!g[i]) continue;
		t=g[i];
		cout<<t->key<<" ";
		while(t->next)
		{
			t=t->next;
			cout<<t->key<<" ";
		}
		cout<<endl;
	}
}
Topic archived. No new replies allowed.