Selasa, 30 November 2010

Sample Program Linked List

#include"stdio.h"

struct simpul
{
int data;
struct simpul *next;
};

struct simpul *awal=NULL, *akhir=NULL,*baru;



main()
{
int pil;
do
{
clrscr();
printf("\nMENU LINKED LIST");
printf("\n1. TAMBAH BELAKANG");
printf("\n9. SELESAI");
printf("\nPilihan anda [1..9] : ");scanf("%d",&pil);
if(pil==1)
{
tambah_belakang();
tampil();
getch();
}
}
while (pil!=9);
getch();
}




tambah_belakang()
{
baru=(struct simpul*) malloc(sizeof (struct simpul));
printf("Input nilai : ");scanf("%d",&baru->data);
baru->next=NULL;

if(awal==NULL)
{
awal=baru;
akhir=baru;
}
else
{
akhir->next=baru;
akhir=baru;
}
}

tampil()
{
if(awal==NULL)
printf("LINKED LIST KOSONG");
else
{
baru=awal;
printf("\n\n");
while(baru!=NULL)
{
printf(" %d ",baru->data);
baru=baru->next;
}
}
}