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
|
#include<iostream.h>
const n=10;
struct link
{ char key; link*next;}*g[n];
void init(link*g[n])
{ for(int i=0; i<n; i++)
g[i]=NULL; }
int search_node(link*g[n],char c)
{int flag=0;
for(int i=0; i<n; i++)
if(g[i])
if(g[i]->key==c)flag=1;
return flag;
}
int search_arc(link*g[n], char c1, char c2)
{ int flag=0;
if(search_node(g,c1)&&search_node(g,c2))
{ int i=0;
while(g[i]->key!=c1)i++;
link*p=g[i];
while (p->key!=c2&&p->next!=NULL)
p=p->next;
if (p->key==c2) flag=1;
}
return flag;
}
void del_node(link*g[n], char c)
{
if(search_node(g, c))
{
int i=0;
while(g[i]->key!=c)
i++;
link*p, *q;
while(g[i]!=NULL)
{ p=g[i];
g[i]=p->next;
delete p;
}
for(i=0; i<n; i++)
if(g[i])
{
p=g[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!";
}
void add_node(link *g[n],char c)
{ if(search_node(g,c))
cout<<"\n Existing node!\n";
else{int j=0;
while(g[j]&&(j<n)) j++;
if(g[j]==NULL){
g[j]=new link;
g[j]->key=c;
g[j]->next=NULL;}
else cout<<"\nOverflow!\n";
}
}
void add_arc(link*g[n], char c1, char c2)
{int i=0;
link*p;
if (search_arc(g, c1, c2))
cout<<"Existing 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 link;
p->key=c2;
p->next=g[i]->next;
g[i]->next=p;
}
}
void print()
{link *p;
for(int i=0;i<n;i++)
{
p=g[i];
cout<<'\n';
while(p!=NULL)
{cout<<p->key<<"->";
if(p->next)
cout<<"->";
p=p->next;
}
}
}
void izoliran(link*g[n]);
void main()
{ char c, k;
int m;
do
{ cout<<"\n Menu\n";
cout<<"1-Initialisation\n";
cout<<"2-Insert node\n";
cout<<"3-Insert arc\n";
cout<<"4-Print\n";
cout<<"5-Find isolated node\n";
cout<<"6-Del node\n";
cout<<"10-Exit";
cout<<"\n Your choice is: ";
cin>>m;
switch(m){
case(1):{init(g);break;}
case(2):{cout<<"Input c ";cin>>c;add_node(g,c);break;}
case(3):{cout<<"Input first node: ";cin>>c;cout<<"Input second node: ";cin>>k;add_arc(g,c,k);break;}
case(4):{print();break;}
case(5):{izoliran(g); break;}
case(6):{cout<<"delete node: ";cin>>c;del_node(g,c);break;}}
}while(m!=10);
}
/*void izoliran(link*g[n])
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(g[i]->key!=g[j]->key)
if(search_arc(g, g[i]->key, g[j]->key))
{
cout<<"The node "<<g[i]->key<<" is not isolated!";
break;
}
}
}
cout<<"The node is isolated!";
}*/
|