Saturday, March 31, 2012

. c Multi Linked List

#include "tp5.h"


addressParent alokasiParent(InfoParent x)
{
    addressParent p;
    p=(addressParent)malloc(sizeof(ElmtListparent));
    if(p!=nil)
    {
        strcpy(info(p),x);
        next(p)=nil;
        prev(p)=nil;
        child(p)=nil;
    }
        else
        {
            printf("Pengalokasian gagal");
        }
    return p;
}

addressChild alokasiChild(InfoChild x)
{
    addressChild q;
    q=(addressChild)malloc(sizeof(ElmtListchild));
    if(q!=nil)
    {
        strcpy(info(q),x);
        next(q)=nil;
        prev(q)=nil;
    }
        else
        {
            printf("Pengalokasian Gagal");
        }
    return q;
}

void CreateList(List *L)
{
    first(*L)=nil;
    last(*L)=nil;
}

void InsertFirstParent(List *L,addressParent P)
{
    if((first(*L)==nil)&&(last(*L)==nil))
    {
        first(*L)=P;
        last(*L)=P;
        printf("insert parent berhasil");
    }
        else
        {
            next(P)=first(*L);
            prev(first(*L))=P;
            first(*L)=P;
            printf("insert parent berhasil");
        }
}

void InsertFirstChild(addressParent *P, addressChild Q)
{

    if(child(*P)==nil)
    {
        child(*P)=Q;
        printf("Insert Child Berhasil");
    }
        else
        {
            next(Q)=child(*P);
            prev(child(*P))=Q;
            child(*P)=Q;
            printf("Insert Child Berhasil");
        }
}

void View(List L)
{
    addressParent p;
    addressChild q;
    if(first(L)==nil)
    {
        printf("Maaf List Kosong");
    }
        else
        {
            p=first(L);
            do
            {
                printf("Parentnya : %s\n",info(p));
                if(child(p)!=nil)
                {
                    q=child(p);
                    do
                    {
                        printf("   Childnya : %s\n",info(q));
                        q=next(q);
                    }while(q!=nil);
                }
                p=next(p);
            }while(p!=nil);
        }
}

addressParent search(List L, InfoParent x)
{
    addressParent p;
    p=first(L);
    if (first(L)==nil)
    {
        return nil;
    }
        else
        {
            while((next(p)!=nil)&&(strcmp(info(p),x)!=0))
            {
                p=next(p);
            }
            if(strcmp(info(p),x)==0)
            {
                return p;
            }
                else if((strcmp(info(p),x)!=0)&&(next(p)==nil))
                {
                    return nil;
                }
        }
}

void DeleteFirstParent(List *L)
{
    addressParent p;
    if(first(*L)==nil)
    {
        printf("List Kosong");
    }
        else
        {
            if(first(*L)==last(*L))
            {
                p=first(*L);
                first(*L)=nil;
                last(*L)=nil;
                child(p)=nil;
                free(p);
                free(child(p));
            }
                else
                {
                    p=first(*L);
                    first(*L)=next(p);
                    next(p)=nil;
                    prev(first(*L))=nil;
                    child(p)=nil;
                    free(p);
                    free(child(p));
                }
            printf("Data Berhasil Di hapus");
        }
}


No comments:

Post a Comment