#include<stdio.h>
#include<malloc.h>
//-----------------------------------------
struct simpul{
char info;
struct simpul *next;
struct jalur *arc;
};
struct jalur{
int info;
struct jalur *next;
struct simpul *node;
};
struct graph{
struct simpul *first;
};
//------------------------------------------
void createEmpty(graph *G){
(*G).first = NULL;
}
//------------------------------------------
void addSimpul(char c, graph*G){
simpul *node;
node = (simpul *) malloc
(sizeof (simpul));
node->info = c;
node->next = NULL;
node->arc = NULL;
if((*G).first == NULL){
/*jika graph kosong*/
(*G).first = node;
}
else{
/*menambahkan simpul baru pada akhir graph*/
simpul *last = (*G).first;
while(last->next !=NULL){
last = last->next;
}
last->next = node;
}
}
//----------------------------------------------
void addJalur(simpul *tujuan, int beban, simpul **awal){
jalur *arc;
arc = (jalur *) malloc
(sizeof (jalur));
arc->info = beban;
arc->next = NULL;
arc->node = tujuan;
if((*awal)->arc == NULL){
/*jika list jalur kosong*/
(*awal)->arc = arc;
}
else{
/*menambahkan jalur baru pada akhir list jalur*/
jalur *last = (*awal)->arc;
while(last->next !=NULL){
last = last->next;
}
last->next = arc;
}
}
//----------------------------------------------
void delSimpul(char c, graph*G)
{
simpul *elmt = (*G).first;
if(elmt !=NULL)
{
simpul *prec = NULL;
/*mencari simpul yang akan dihapus*/
bool ketemu = false;
while((elmt !=NULL) && (ketemu == false))
{
if(elmt->info == c)
{
ketemu = true;
}
else{
prec = elmt;
elmt = elmt->next;
}
}
if(ketemu == true){
if(prec == NULL){
/*hapus simpul pertama*/
(*G).first = elmt->next;
}
else{
if(elmt->next == NULL){
/*hapus simpul terakhir*/
prec->next = NULL;
}
else{
/*hapus simpul ditengah*/
prec->next = elmt->next;
elmt->next = NULL;
}
}
free(elmt);
}
else{
printf("tidak ada simpul dengan info karakter masukan\n");
}
}
}
//---------------------------------------------------
simpul* findSimpul(char c, graph G)
{
simpul *hasil = NULL;
simpul *node = G.first;
int ketemu = 0;
while((node !=NULL) &&(ketemu == 0))
{
if(node->info == c)
{
hasil = node;
ketemu = 1;
}
else
{
node = node->next;
}
}
return hasil;
}
//----------------------------------------------------
void delJalur(char ctujuan, simpul **awal)
{
jalur *arc = (*awal)->arc;
if(arc != NULL)
{
jalur *prec = NULL;
/*mencari jalur yang akan dihapus*/
int ketemu = 0;
while ((arc !=NULL) &&(ketemu == 0))
{
if(arc->node->info == ctujuan)
{
ketemu = 1;
}
else
{
prec = arc;
arc = arc->next;
}
}
if(ketemu == 1)
{
if(prec == NULL)
{
/*hapus jalur pertama*/
(*awal)->arc = arc->next;
}
else
{
if(arc->next == NULL)
{
/*hapus jalur terakhir*/
prec->next = NULL;
}
else
{
/*hapus jalur ditengah*/
prec->next = arc->next;
arc->next = NULL;
}
}
}
free(arc);
}
else{
printf("tidak ada jalur dengan simpul tujuan\n");
}
}
//--------------------------------------------------
void printGraph(graph G){
simpul *node = G.first;
if(node != NULL){
while(node != NULL){
printf("simpul : %c\n",node->info);
jalur *arc = node->arc;
while(arc != NULL){
printf(" -add jalur ke simpul : %c dengan beban : %d\n", arc->node->info, arc->info);
arc = arc->next;
}
node = node->next;
}
}
else{
printf("graph kosong\n");
}
}
//--------------------------------------------------
int main(){
struct graph G;
createEmpty(&G);
addSimpul('A' , &G);
addSimpul('B' , &G);
addSimpul('C' , &G);
addSimpul('D' , &G);
addSimpul('E' , &G);
addSimpul('F' , &G);
simpul *begin = findSimpul('A', G);
simpul *end = findSimpul('B', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
begin = findSimpul('B', G);
end = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
end = findSimpul('E', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 7, &begin);
}
begin = findSimpul('C', G);
end = findSimpul('A', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
begin = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 8, &begin);
}
end = findSimpul('C', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
begin = findSimpul('E', G);
end = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 4, &begin);
}
end = findSimpul('F', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 4, &begin);
}
begin = findSimpul('F', G);
end = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 2, &begin);
}
printf("\n=================================================\n");
printf("\n=================================================\n");
printf("\n RAHMAT AGUS RUSADI\n");
printf("\n 2010140487\n");
printf("\n=================================================\n");
printf("\n=================================================\n");
printGraph(G);
printf("\n=================================================\n");
begin = findSimpul('A', G);
if((begin !=NULL)){
delJalur('B', &begin);
}
printf("setelah dihapus\n");
printGraph(G);
printf("\n=================================================\n");
system("PAUSE");
return 0;
}
#include<malloc.h>
//-----------------------------------------
struct simpul{
char info;
struct simpul *next;
struct jalur *arc;
};
struct jalur{
int info;
struct jalur *next;
struct simpul *node;
};
struct graph{
struct simpul *first;
};
//------------------------------------------
void createEmpty(graph *G){
(*G).first = NULL;
}
//------------------------------------------
void addSimpul(char c, graph*G){
simpul *node;
node = (simpul *) malloc
(sizeof (simpul));
node->info = c;
node->next = NULL;
node->arc = NULL;
if((*G).first == NULL){
/*jika graph kosong*/
(*G).first = node;
}
else{
/*menambahkan simpul baru pada akhir graph*/
simpul *last = (*G).first;
while(last->next !=NULL){
last = last->next;
}
last->next = node;
}
}
//----------------------------------------------
void addJalur(simpul *tujuan, int beban, simpul **awal){
jalur *arc;
arc = (jalur *) malloc
(sizeof (jalur));
arc->info = beban;
arc->next = NULL;
arc->node = tujuan;
if((*awal)->arc == NULL){
/*jika list jalur kosong*/
(*awal)->arc = arc;
}
else{
/*menambahkan jalur baru pada akhir list jalur*/
jalur *last = (*awal)->arc;
while(last->next !=NULL){
last = last->next;
}
last->next = arc;
}
}
//----------------------------------------------
void delSimpul(char c, graph*G)
{
simpul *elmt = (*G).first;
if(elmt !=NULL)
{
simpul *prec = NULL;
/*mencari simpul yang akan dihapus*/
bool ketemu = false;
while((elmt !=NULL) && (ketemu == false))
{
if(elmt->info == c)
{
ketemu = true;
}
else{
prec = elmt;
elmt = elmt->next;
}
}
if(ketemu == true){
if(prec == NULL){
/*hapus simpul pertama*/
(*G).first = elmt->next;
}
else{
if(elmt->next == NULL){
/*hapus simpul terakhir*/
prec->next = NULL;
}
else{
/*hapus simpul ditengah*/
prec->next = elmt->next;
elmt->next = NULL;
}
}
free(elmt);
}
else{
printf("tidak ada simpul dengan info karakter masukan\n");
}
}
}
//---------------------------------------------------
simpul* findSimpul(char c, graph G)
{
simpul *hasil = NULL;
simpul *node = G.first;
int ketemu = 0;
while((node !=NULL) &&(ketemu == 0))
{
if(node->info == c)
{
hasil = node;
ketemu = 1;
}
else
{
node = node->next;
}
}
return hasil;
}
//----------------------------------------------------
void delJalur(char ctujuan, simpul **awal)
{
jalur *arc = (*awal)->arc;
if(arc != NULL)
{
jalur *prec = NULL;
/*mencari jalur yang akan dihapus*/
int ketemu = 0;
while ((arc !=NULL) &&(ketemu == 0))
{
if(arc->node->info == ctujuan)
{
ketemu = 1;
}
else
{
prec = arc;
arc = arc->next;
}
}
if(ketemu == 1)
{
if(prec == NULL)
{
/*hapus jalur pertama*/
(*awal)->arc = arc->next;
}
else
{
if(arc->next == NULL)
{
/*hapus jalur terakhir*/
prec->next = NULL;
}
else
{
/*hapus jalur ditengah*/
prec->next = arc->next;
arc->next = NULL;
}
}
}
free(arc);
}
else{
printf("tidak ada jalur dengan simpul tujuan\n");
}
}
//--------------------------------------------------
void printGraph(graph G){
simpul *node = G.first;
if(node != NULL){
while(node != NULL){
printf("simpul : %c\n",node->info);
jalur *arc = node->arc;
while(arc != NULL){
printf(" -add jalur ke simpul : %c dengan beban : %d\n", arc->node->info, arc->info);
arc = arc->next;
}
node = node->next;
}
}
else{
printf("graph kosong\n");
}
}
//--------------------------------------------------
int main(){
struct graph G;
createEmpty(&G);
addSimpul('A' , &G);
addSimpul('B' , &G);
addSimpul('C' , &G);
addSimpul('D' , &G);
addSimpul('E' , &G);
addSimpul('F' , &G);
simpul *begin = findSimpul('A', G);
simpul *end = findSimpul('B', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
begin = findSimpul('B', G);
end = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
end = findSimpul('E', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 7, &begin);
}
begin = findSimpul('C', G);
end = findSimpul('A', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
begin = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 8, &begin);
}
end = findSimpul('C', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 3, &begin);
}
begin = findSimpul('E', G);
end = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 4, &begin);
}
end = findSimpul('F', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 4, &begin);
}
begin = findSimpul('F', G);
end = findSimpul('D', G);
if((begin !=NULL)&&(end !=NULL)){
addJalur(end, 2, &begin);
}
printf("\n=================================================\n");
printf("\n=================================================\n");
printf("\n RAHMAT AGUS RUSADI\n");
printf("\n 2010140487\n");
printf("\n=================================================\n");
printf("\n=================================================\n");
printGraph(G);
printf("\n=================================================\n");
begin = findSimpul('A', G);
if((begin !=NULL)){
delJalur('B', &begin);
}
printf("setelah dihapus\n");
printGraph(G);
printf("\n=================================================\n");
system("PAUSE");
return 0;
}
Tidak ada komentar