You are on page 1of 2

#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include <stdio.h> #include <conio.h> #include "boolean.

h" #define Nil NULL #define info(p) (p)->info #define next(p) (p)->next #define first(l) (l).first typedef int infotype; typedef struct tElmlist *address; typedef struct tElmlist { infotype info; address next; } elmlist; typedef struct { address first; } list; void creatlist (list *l); address alokasi (infotype x); //pesan tempat void dealokasi (address p); boolean isEmpty (list l); void insertFirst (list *l, address p); void insertLast (list *l, address p); void insertAfter (address q, address p); void view (list l); address search (list l, infotype x); #endif // LIST_H_INCLUDED //header boolean #ifndef BOOLEAN_H_INCLUDED #define BOOLEAN_H_INCLUDED typedef unsigned int boolean; #define false 0 #define true !false #endif // BOOLlEAN_H_INCLUDED 1

// Implementasi #include "list.h" #include "malloc.h" void creatlist (list *l){ (*l).first =Nil; } address alokasi (infotype x) { address p = (address)malloc(sizeof(elmlist)); info (p) = x; next (p) = Nil; return p; } void dealokasi (address p) { free (p); } boolean isEmpty (list l) { return first(l)=Nil ; } void insertFirst (list *l, address p) { next(p)=first(*l); first(*l)=p; } void insertLast (list *l, address p){ if (isEmpty(*l)) insertFirst(&(*l),p); else { address q=first(*l); while (next(q)!=Nil); q = next(q); insertAfter(q,p); } } void insertAfter (address q, address p){ next(p)=next(q); next(q)=p; }

//lanjutan implementasi void view (list l){ address q = first(l); while (q !=Nil){ printf("%d\n",info(q)); q=next(q); } } address search (list l, infotype x){ address q = first(l); while (q !=Nil) if (x== info(q)) return q; else q=next(q); return Nil; } //main #include <stdio.h> #include "boolean.h" int main (){ list l; creatlist(&l); int pil; infotype x; address p; do { printf ("1. Insert First\n"); printf ("2. Insert Last\n"); printf ("3. Insert After\n"); printf ("4. Insert View\n"); printf ("Pilihan: "); scanf ("%d",&pil); 2

//lanjutan main printf ("\n"); switch (pil); case 1: printf ("Info: "); scanf ("%d",&x); insertFirst(&l,alokasi(x)); printf ("Data berhasil di insert"); break; case 2: //printf ("Info: "); //scanf ("%d",&x); //insertFirst(&l,alokasi(x)); //printf ("Data berhasil di insert");break; case 3: break; case 4: view (l); break; default: printf ("Pilihan Salah");' } while () getch (); }

You might also like