Anda di halaman 1dari 10

ASKISI 9.cpp #include <iostream> #include <fstream> #include "GGraph.h" #include "LinkedStack.h" #include "LList.

h" using namespace std; LIST * S!"G#$%& *G' char (); const int *a+,har-.////; (oid main") 0 G#$%& *G - ne1 G#$%&; 223 G4>si5e-/; G4>head-DELL;

223 6789:;<=>? @:; A<BC:; 223

int lineDo-/' i' F' counter; char node.' nodeG' strH*a+,harI; 226789:;<=J> @:K =<BC: 8J @7 L<MN7 N;KOJOJ8PK7Q R>N@?Q ?ST R>N@JQ ifstream infile""input.t+t"); 1hile "infile.getline"str'*a+,har4.)) 0 lineDoUU; if "lineDo--.) 0 for "i-/;i<strlen"str);iUU) 0 if "strHiIV-W W) 0 %X$Y *% - ne1 %X$Y; %4>data-strHiI; insertLast%eak"G'%); 3 3 3 else 0 node.-strH/I; nodeG-strHGI; %X$Y *temp; .

temp-find%eak"G'node.); if "tempV-DELL) 0 if "findDeighZor"temp'nodeG)--DELL) 0 DXIG&[\# *D - ne1 DXIG&[\#; D4>data-nodeG; insertLastDeighZor"temp'D); 3 3 temp-find%eak"G'nodeG); if "tempV-DELL) 0 if "findDeighZor"temp'node.)--DELL) 0 DXIG&[\# *D - ne1 DXIG&[\#; D4>data-node.; insertLastDeighZor"temp'D); 3 3 3 infile.close"); 22 ]RP=LJ9 ?K : =<BC:Q J>K?9 N;KOJOJ8PK:Q ^?9 J8C?K>_J9 ^?@BRR7R: 8MK;8? %X$Y *%.; LIST *L; %.-G4>head; for"i-/; i<G4>si5e; iUU) 0 L- S!"G' %.4>data); if "L4>si5eV-G4>si5e) 0 cout <<endl<< "This graph is D\T connected"<<endl<<endl; s`stem""pause"); return; 3 %.-%.4>ne+t; 3 cout <<endl<< "The graph is connected"<<endl<<endl; s`stem""pause"); G 3

3 LIST * S!"G#$%& *G' char () 0 LIST *L - ne1 LIST; L4>si5e-/; L4>head-DELL; int i; SD\ X *t; %X$Y *%; DXIG&[\# *D; ST$,Y *S - ne1 ST$,Y; 223 S4>si5e-/; S4>head-DELL; SD\ X *sn - ne1 SD\ X; sn4>data-(; push"S' sn); D\ X *n - ne1 D\ X; n4>data - (; insertLast"L' n); 1hile "S4>si5e>/) 0 t-pop"S); if "findDode"L' t4>data)--DELL) 22J9NB=J9 N@7 R>N@? "?K OJK ;SB<LJ9 MO7) @: N@:9LJ>: S:; P^?KJ pop 0 D\ X *n - ne1 D\ X; n4>data - t4>data; insertLast"L' n); 3 %-find%eak"G' t4>data); D - %4>head; for "i-/; i<%4>si5e; iUU) 22J9NB=J9 N@7 N@:>c? "?K OJK ;SB<L:;K MO7 N@7 R>N@? M @7 N@:>c?) 0 22TR:;Q @:;Q =J9@dK9^:eQ ^T8c:;Q @:; N@:9LJ>:; S:; S<:P^;fJ ?ST @7K pop if "findSDode"S' D4>data)--DELL gg findDode"L' D4 >data)--DELL) 0 h

223 223 6789:;<=>? @:; a>N@?Q 223

223 6789:;<=>? @:; b@:>c?Q 223

SD\ X *sn - ne1 SD\ X; sn4>data-D4>data; push"S' sn); 3 D-D4>ne+t; 3 return L; 3

LinkedStack.h t`pedef struct snode0 char data; 22int line; struct snode *ne+t; 3 SD\ X; t`pedef struct stack0 SD\ X *head; int si5e; 3 ST$,Y; (oid push"ST$,Y *L' SD\ X *ne1Dode); SD\ X *pop"ST$,Y *L); SD\ X *peek"ST$,Y *L); int isXmpt`"ST$,Y *L); SD\ X *findSDode"ST$,Y *L' char element); LinkedStack.cpp #include <iostream> #include "LinkedStack.h" using namespace std; (oid push"ST$,Y *L' SD\ X *ne1Dode) 0 ne1Dode4>ne+t-L4>head; L4>head-ne1Dode; L4>si5eUU; 3 i

SD\ X *pop"ST$,Y *L) 0 if "isXmpt`"L)) 0 cout << "The stack is empt`"<<endl; return DELL; 3 SD\ X *p; p-L4>head; L4>head-p4>ne+t; L4>si5e44; return p; 3 LList.h t`pedef struct node0 char data; struct node *ne+t; 3 D\ X; t`pedef struct list0 D\ X *head; int si5e; 3 LIST; (oid insert!irst "LIST *L' D\ X *ne1Dode); (oid insertLast "LIST *L' D\ X *ne1Dode); D\ X *last"LIST *L); D\ X *findDode"LIST *L' char element); Zool isXmpt`"LIST *L); (oid deleteDode "LIST *L' char element); int si5e"LIST *L); LList.cpp

#include <iostream> #include "LList.h" using namespace std; (oid insert!irst "LIST *L' D\ X *ne1Dode) 0 ne1Dode4>ne+t-L4>head; j

L4>head-ne1Dode; L4>si5eUU;

(oid insertLast "LIST *L' D\ X *ne1Dode) 0 if "L4>si5e--/) 0 L4>head-ne1Dode; ne1Dode4>ne+t-DELL; 3 else 0 D\ X *p; p-last"L); p4>ne+t-ne1Dode; ne1Dode4>ne+t-DELL; 3 L4>si5eUU; 3 D\ X *last"LIST *L) 0 int i; D\ X *p; p-L4>head; for "i-/;i<L4>si5e4.;iUU) p-p4>ne+t; return p; 3 D\ X *findDode"LIST *L' char element) 0 if "L4>si5e--/) return DELL; int i; D\ X *p; p-L4>head; for "i-/;i<L4>si5e;iUU) 0 if "p4>data--element) return p; p-p4>ne+t; 3 k

return DELL; 3 Zool isXmpt`"LIST *L) 0 if "L4>si5e--/) return true; return false; 3 (oid deleteDode "LIST *L' char element) 0 D\ X *p' *p.; p-findDode"L' element); if "pV-DELL) 0 int i-/; p.-L4>head; 1hile "p.4>dataV-element) 0 p.-p.4>ne+t; iUU; 3 if "i--/) L4>head-p4>ne+t; else 0 p.-L4>head; for "int F-.;F<i;FUU) p.-p.4>ne+t; p.4>ne+t-p4>ne+t;

3 L4>si5e44; delete p; 3 3

int si5e"LIST *L) 0 return L4>si5e; 3 l

GGraph.h t`pedef struct neighZor0 char data; struct neighZor *ne+t; 3 DXIG&[\#; t`pedef struct peak0 char data; DXIG&[\# *head; struct peak *ne+t; int si5e; 3 %X$Y; t`pedef struct graph0 %X$Y *head; int si5e; 3 G#$%&; (oid insertLast%eak"G#$%& *G' %X$Y *ne1%eak); %X$Y *last%eak"G#$%& *G); %X$Y *find%eak"G#$%& *G' char element); (oid insertLastDeighZor"%X$Y *%' DXIG&[\# *ne1DeighZor); DXIG&[\# *lastDeighZor"%X$Y *%); DXIG&[\# *findDeighZor"%X$Y *%' char element); GGraph.cpp #include <iostream> #include "GGraph.h" using namespace std; (oid insertLast%eak"G#$%& *G' %X$Y *ne1%eak) 0 if "G4>si5e--/) 0 G4>head-ne1%eak; ne1%eak4>ne+t-DELL; ne1%eak4>head-DELL; ne1%eak4>si5e-/; 3 else m

0 %X$Y *p; p-last%eak"G); p4>ne+t-ne1%eak; ne1%eak4>ne+t-DELL; ne1%eak4>head-DELL; ne1%eak4>si5e-/; 3 G4>si5eUU; 3 %X$Y *last%eak"G#$%& *G) 0 int i; %X$Y *p; p-G4>head; for "i-/;i<G4>si5e4.;iUU) p-p4>ne+t; return p; 3 (oid insertLastDeighZor"%X$Y *%' DXIG&[\# *ne1DeighZor) 0 if "%4>si5e--/) 0 %4>head-ne1DeighZor; ne1DeighZor4>ne+t-DELL; 3 else 0 DXIG&[\# *p; p-lastDeighZor"%); p4>ne+t-ne1DeighZor; ne1DeighZor4>ne+t-DELL; 3 %4>si5eUU; 3 DXIG&[\# *lastDeighZor"%X$Y *%) 0 int i; DXIG&[\# *p; p-%4>head; for "i-/;i<%4>si5e4.;iUU) n

p-p4>ne+t; return p;

%X$Y *find%eak"G#$%& *G' char element) 0 if "G4>si5e--/) return DELL; int i; %X$Y *p; p-G4>head; for "i-/;i<G4>si5e;iUU) 0 if "p4>data--element) return p; p-p4>ne+t; 3 return DELL; 3 DXIG&[\# *findDeighZor"%X$Y *%' char element) 0 if "%4>si5e--/) return DELL; int i; DXIG&[\# *p; p-%4>head; for "i-/;i<%4>si5e;iUU) 0 if "p4>data--element) return p; p-p4>ne+t; 3 return DELL; 3

./