Graphh delete
#include <stdio.h>
#include <malloc.h> struct vertex { struct edge * jalur; int data; struct vertex * nextvertex; }; struct edge { struct vertex * tujuan; struct edge * nextedge; int beban; }; struct graph { struct vertex * first; }; void newgraph(struct graph * g) { g->first = NULL; } void buatvertex(struct graph * g,int data) { struct vertex * baru = (struct vertex*)malloc(sizeof(struct vertex)); baru->data = data; baru->jalur = NULL; baru->nextvertex = NULL; if(g->first == NULL) { g->first = baru; }else { struct vertex * akhir = g->first; while(akhir->nextvertex != NULL) { akhir = akhir->nextvertex; } akhir->nextvertex = baru; } } void buatedge(struct vertex * awal,struct vertex * tujuan,int beban) { struct edge * buatedge = (struct edge*)malloc(sizeof(struct edge)); buatedge->beban = beban; buatedge->tujuan = tujuan; buatedge->nextedge = NULL; if(awal->jalur == NULL) { awal->jalur = buatedge; }else { struct edge * akhir = awal->jalur; while(akhir->nextedge != NULL) { akhir = akhir->nextedge; } akhir->nextedge = buatedge; } } void print(struct graph * g) { struct vertex * temp = g->first; if(temp != NULL) { printf("Data simpul \n"); while(temp != NULL) { printf("simpul %d \n",temp->data); struct edge * tempedge = temp->jalur; while(tempedge != NULL) { printf("%d , %d \n",tempedge->tujuan->data,tempedge->beban); tempedge = tempedge->nextedge; } temp = temp->nextvertex; } }else { printf("kosong"); } } struct vertex * cari(int data,struct graph g) { struct vertex * cari = g.first; int ketemu = 0; struct vertex * hasil = NULL; while(ketemu == 0 && hasil == NULL ) { if(cari->data == data) { ketemu = 1; hasil = cari; }else { cari = cari->nextvertex; } } return hasil; } void delall(struct vertex *g) { struct edge * konz = g->jalur; struct edge * hapus = NULL; while(konz != NULL) { hapus = konz; konz = konz->nextedge; g->jalur = konz; hapus->nextedge = NULL; free(hapus); } } void deledge(int tujuan,struct vertex * g) { struct edge * hapus = g->jalur; if(hapus != NULL) { struct edge * prev = NULL; int ketemu = 0; while(hapus != NULL && ketemu == 0) { if(hapus->tujuan->data = tujuan) { ketemu = 1; }else { prev = hapus; hapus = hapus->nextedge; } }if(ketemu == 1) { if(prev == NULL) { g->jalur = hapus->nextedge; hapus->nextedge = NULL; }else { if(hapus->nextedge == NULL) { prev->nextedge = NULL; }else { prev->nextedge = hapus->nextedge; hapus->nextedge = NULL; } } free(hapus); } } } void delvertex(int c, struct graph * g) { struct vertex *hapus = g->first; if(hapus != NULL) { struct vertex * prev = NULL; int ketemu = 0; while(hapus != NULL && ketemu == 0) { if(hapus->data == c) { ketemu = 1; }else { prev = hapus; hapus = hapus->nextvertex; } } if(ketemu == 1) { struct vertex * bantu = g->first; while(bantu != NULL) { if(bantu != hapus) { deledge(hapus->data,bantu); } bantu = bantu->nextvertex; } delall(hapus); if(ketemu == 1) { if(prev == NULL) { g->first = hapus->nextvertex; hapus->nextvertex = NULL; }else { if(hapus->nextvertex == NULL) { prev->nextvertex = NULL; }else { prev->nextvertex = hapus->nextvertex; hapus->nextvertex = NULL; } } free(hapus); } } } } int main() { struct graph g; newgraph(&g); struct vertex *a1,*a2; buatvertex(&g,10); buatvertex(&g,9); a1 = cari(10,g); a2 = cari(9,g); buatedge(a1,a2,100); print(&g); print(&g); a1 = cari(10,g); deledge(9,a1); print(&g); }
Komentar
Posting Komentar