Jumat, 08 Januari 2010

list

#include

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

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

void CreateList (List *L)
{ first(*L)=nil; }
boolean IsEmptyList (List L)
{ return (first(L)==nil); }
void PrintList (List L)
{ address P;
if(IsEmptyList(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 InsertList (List *L, InfoType E)
{ address P;
Alokasi(&P);
if(P!=nil)
{ info(P)=E;
next(P)=first(*L);
first(*L)=P;
}
}
void DeleteList (List *L, InfoType *E)
{ address P;
if(!IsEmptyList(*L))
{ P=first(*L);
(*E)=info(P);
first(*L)=next(P);
Dealokasi(P);
printf("\n\nhapus list");
}
}


/*ALGORITMA*/
main()
{ List A; int x;
clrscr();
CreateList(&A); PrintList(A);
InsertList(&A,8); PrintList(A);
InsertList(&A,1); PrintList(A);
InsertList(&A,5); PrintList(A);
DeleteList(&A,&x); PrintList(A);
DeleteList(&A,&x); PrintList(A);
DeleteList(&A,&x); PrintList(A);
getch();
}

Tidak ada komentar:

Posting Komentar