GRAPH

 #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 graph *g)

{

    struct vertex * cari = g->first;

    struct vertex * hapus;

    while(cari != NULL)

    {

        hapus = cari;

        cari = cari->nextvertex;

        g->first  = cari;

        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);

    delall(&g);

    print(&g);

    

    

    

}


Komentar

Postingan populer dari blog ini

Pentingnya Mendapatkan Sertifikasi di Bidang IT/Sebagai Programmer

Pengantar Java Swing Bagian 1

Pengenalan UML