Saturday, March 31, 2012

main program Multi LInked List

#include <stdio.h>
#include <stdlib.h>
#include "tp5.c"

int main()
{
    int pil;
    InfoParent x,cari;
    InfoChild y;
    char dump;
    List A;
    addressParent p;
    addressChild q;
    CreateList(&A);

    do
    {
        system("cls");
        printf("Menu Multi List\n");
        printf("1. Insert First\n");
        printf("2. Insert Child\n");
        printf("3. Delete Parent\n");
        printf("4. View\n");
        printf("5. Exit\n");
        printf("PILIHAN <1-5> : ");
        scanf("%d",&pil);
        scanf("%c",&dump);
        switch(pil)
        {
            case 1 :
                        printf("Masukan Parentnya : ");
                        scanf("%s",&x);
                        p=alokasiParent(x);
                        InsertFirstParent(&A,p);
                        break;
            case 2 :
                        printf("Masukan Parentnya : ");
                        scanf("%s",&cari);
                        p=search(A,cari);
                        if(first(A)==nil)
                        {
                            printf("List Kosong");
                        }
                            else
                            {
                                if(p==nil)
                                {
                                    printf("Parent tidak ada");
                                }
                                    else
                                    {
                                        printf("Masukan Childnya : ");
                                        scanf("%s",&y);
                                        q=alokasiChild(y);
                                        InsertFirstChild(&p,q);
                                    }
                            }
                        break;
            case 3 :
                        DeleteFirstParent(&A);
                        break;
            case 4 :
                        system("cls");
                        View(A);
                        break;
            case 5 :

                        printf("Terima Kasih");
                        break;
            default :
                        printf("Keyword Salah");
        }getche();
    }while(pil!=5);
    return 0;
}
Read moremain program Multi LInked List

. 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");
        }
}


Read more. c Multi Linked List

.h Multi Linked List

#ifndef TP5_H_INCLUDED
#define TP5_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define nil NULL
#define info(p) (p)->info
#define next(p) (p)->next
#define prev(p) (p)->prev
#define child(p) (p)->child
#define first(L) ((L).first)
#define last(L) ((L).last)

typedef char InfoParent[15];
typedef char InfoChild[15];

typedef struct tElmtParent *addressParent;
typedef struct tElmtChild *addressChild;
typedef struct tElmtParent
{
    InfoParent info;
    addressParent next;
    addressParent prev;
    addressChild child;
}ElmtListparent;

typedef struct tElmtChild
{
    InfoChild info;
    addressChild next;
    addressChild prev;
}ElmtListchild;

typedef struct
{
    addressParent first;
    addressParent last;
}List;

addressParent search(List L, InfoParent x);
int createInfoParent(InfoParent x);//memasukansuatuinputankesebuahInfotypeParent
int createInfoChild(InfoChild y);//memasukansuatuinputankesebuahInfotypeChild
addressParent ParentalokasiParent(InfoParent x);//mengalokasikan sebuah InfoParent kedalam sebuah AddressParent
addressChild ChildalokasiChild(InfoChild x);//mengalokasikan sebuah InfoChild kedalam sebuah AddressChild
void CreateList(List *L);//membuat list baru
void InsertFirstParent(List *L,addressParent P);//menambahkan Element P ke Element Pertama di List Parent
void InsertFirstChild(addressParent *P,addressChild Q); // );//menambahkan Element P ke Element Pertama di List Child
void DeleteFirstParent(List *L); // );//menghapus Element dari Element Pertama di List Parent
void View(List L);//menampilkan semua info pada List

#endif // TP5_H_INCLUDED
Read more.h Multi Linked List

Friday, March 30, 2012

Bagian Main Dari program Double Linked List

#include <stdio.h>
#include <stdlib.h>
#include "tp4.c"
int main()
{
    int bil,pil;
    char dump;
    address p;
    doublelist A;
    createList(&A);
    do
    {
        system("cls");
        printf("Program Tugas Pendahuluan Modul 3\n\n");
        printf("=======================\n");
        printf("1. Insert first\n");
        printf("2. Delete last \n");
        printf("3. Tampil isi List\n");
        printf("4. Exit\n");
        printf("========================\n");
        printf("Pilihan : ");
        scanf("%d",&pil);
        scanf("%c",&dump);

        switch(pil)
        {
            case 1 :
                    printf("masukan bilagan : ");
                    scanf("%d",&bil);
                    p=alokasi(bil);
                    insertFirst(&A,p);
                    printf("Berhasil");
                    break;
            case 2 :
                    deleteLast(&A);
                    printf("Delete");
                    break;
            case 3 :
                    system("cls");
                    printf("Tampil Dari isi list : \n");
                    viewList(A);
                    break;
            case 4 :
                    printf("terima Kasih");
                    break;
            default :
                    printf("kode salah");

        }getche();
    }while(pil!=4);
    return 0;
}
Read moreBagian Main Dari program Double Linked List

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

        }
}
Read moreBagian Contoh *.c Double Link List menggunakan ADT

Bagian Contoh *.h Double Link List menggunakan ADT

#ifndef TP4_H_INCLUDED
#define TP4_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define nil NULL
#define info(p) (p)->info
#define next(p) (p)->next
#define prev(p) (p)->prev
#define first(L) ((L).first)
#define last(L) ((L).last)

typedef int infotype;
typedef struct tElmtList *address;
typedef struct tElmtList
{
    infotype info;
    address next;
    address prev;
}ElmtList;

typedef struct
{
    address first;
    address last;
}doublelist;


void createList (doublelist *L); //membuat list baru
address alokasi (infotype x); //mengirimkan alamat dari alokasi sebuah elemen
void dealokasi (address P); //menghapus pemakaian alamat memory dari P
int isEmpty (doublelist *L); //pengecekan isi dari list, kalau kosong mengembalikan nilai 1, kalau tidak mengembalikan nilai 0
void insertFirst (doublelist *L, address P); //menambahkan elemen P kedalam elemen yang terakhir pada list L
void deleteLast(doublelist *L); //menghapus elemen pertama dari sebuah list L
void viewList (doublelist L); //menampilkan semua isi dari sebuah list


#endif // TP4_H_INCLUDED
Read moreBagian Contoh *.h Double Link List menggunakan ADT