Friday, March 30, 2012

Bagian Contoh *.c Double Link List menggunakan ADT

#include "tp4.h"

void createList (doublelist *L)
{
    first(*L)=nil;
    last(*L)=nil;
}

address alokasi (infotype x)
{
    address p;
    p=(address)malloc(sizeof(ElmtList));
    if(p!=nil)
    {
        info(p)=x;
    }
        else
        {
            printf("pengalokasian gagal ! ! !");
        }
}

void dealokasi (address P)
{
    free(P);
}

int isEmpty (doublelist *L)
{
    if((first(*L)==nil)&&(last(*L)==nil))
    {
        return 1;
    }
        else
        {
            return 0;
        }
}

void insertFirst (doublelist *L, address P)
{
    if(isEmpty(&(*L))==1)
    {
        first(*L)=P;
        last(*L)=P;
        next(P)=nil;
        prev(P)=nil;
    }
        else
        {
            next(P)=first(*L);
            prev(first(*L))=P;
            first(*L)=P;
            prev(P)=nil;
        }
}

void deleteLast(doublelist  *L)
{
    if(next(first(*L))==nil)
    {
        dealokasi(last(*L));
        createList(&(*L));
    }
        else
        {
            last(*L)=prev(last(*L));
            dealokasi(next(last(*L)));
            next(last(*L))=nil;
        }

}

void viewList (doublelist L)
{

    if (isEmpty(&L)==1)
    {
        printf("\nList Kosong");
    }
        else
        {
            address p,q;
            p=first(L);
            q=last(L);
            printf("\nDengan Pointer Next : ");

            do
            {
                printf("%d ",info(p));
                p=next(p);

            }while(p!=nil);

            printf("\n\nDengan Pointer Prev : ");
            do
            {
                printf("%d ",info(q));
                q=prev(q);

            }while(q!=nil);

        }
}

1 comment:

  1. kalo program untuk multy linhed list,dengan kasusnya memisahkan kaya gimana

    ReplyDelete