Jumat, 08 Januari 2010

dlist

#include

#define first(L) (L).first
#define info(P) (P)->info
#define prev(P) (P)->prev
#define next(P) (P)->next
#define nil NULL

typedef int InfoType;
typedef struct tNode *address;
typedef struct tNode {address prev; InfoType info; address next;} Node;
typedef struct {address first;} DList;
typedef enum {false, true} boolean;

void CreateDList (DList *L)
{ first(*L)=nil; }
boolean IsEmptyDList (DList L)
{ return (first(L)==nil); }
void PrintList (DList L)
{ address P;
if(IsEmptyDList(L))
{printf("\nlist kosong");}
else
{ printf("\nCetak List");
P=first(L);
do
{printf("\t%d",info(P));
P=next(P);
}while(P!=nil);
}
}
void Alokasi (address *P)
{(*P) = (address) malloc(sizeof(Node));}
void Dealokasi (address P)
{free(P);}

void Insertfirst (DList *L, InfoType E)
{ address P;
Alokasi(&P);
if(P!=nil)
{ info(P)=E;
prev(P)=nil;
if(IsEmptyDList(*L))
{ next(P)=nil; }
else
{ next(P)=first(*L);
prev(first(*L))=P;
}
first(*L)=P;
}
}
void Insertlast (DList *L, InfoType E)
{ address P, last;
Alokasi(&P);
if(P!=nil)
{ info(P)=E;
prev(P)=nil;
if(IsEmptyDList(*L))
{ next(P)=nil; }
else
{ next(P)=first(*L);
last=first(*L);
while(next(last)!=nil)
{ last=next(last); }
prev(first(*L))=P;
}

}

}
void Deletefirst (DList *L, InfoType *E)
{ address P;
if(IsEmptyDList(*L))
{ (*E)=0; }
else
{ P=first(*L);
(*E)=info(P);
first(*L)=next(P);
if(first(*L)!=nil)
{ prev(first(*L))=nil; }
next(P)=nil;
Dealokasi(P);
}

printf("\n\nhapuslist");
}
void Deletelast (DList *L, InfoType *E)
{ address P;
if(IsEmptyDList(*L))
{ (*E)=0; }
else
{ P=first(*L);
while(next(P)!=nil)
{ P=next(P); }
(*E)=info(P);
next(prev(P))=nil;
prev(P)=nil;
Dealokasi(P);
}

printf("\n\nhapuslist");
}


/*ALGORITMA*/
main()
{ DList A; int x;
clrscr();
CreateDList(&A); PrintList(A);
Insertfirst(&A,8); PrintList(A);
Insertfirst(&A,1); PrintList(A);
Insertfirst(&A,5); PrintList(A);
Insertlast(&A,7); PrintList(A);
Deletefirst(&A,&x); PrintList(A);
Deletelast(&A,&x); PrintList(A);
Deletefirst(&A,&x); PrintList(A);
getch();
}

Tidak ada komentar:

Posting Komentar