Modul 10 Linear Singly Linked List
Modul 10 Linear Singly Linked List
Ada 4 simpul : 1, 2, 3, 4
Setiap simpul terdiri dari 2 elemen/field, yaitu :
INFO : bertipe integer
LINK : bertipe pointer
Simpul no.1 :
Field INFO berisi 10
Field LINK berisi alamat simpul no. 2
Simpul no.1 ditunjuk oleh pointer FIRST
Simpul no.4 ditunjuk oleh pointer LAST
struct SIMPUL{
int INFO;
struct SIMPUL *LINK;
};
SIMPUL *P,*Q,*FIRST,*LAST;
Atang Susila T Informatika UNPAM
Proses
Inisialisasi : persiapan
pembuatan linked list
Membuat simpul awal
Insert simpul kedalam linked list
Delete simpul dari linked list
FIRST = NULL;
LAST = NULL;
P=(SIMPUL*) malloc(sizeof(SIMPUL));
Void AWAL(void)
{
if(FIRST==NULL)
{
FIRST=P;
LAST=P;
P->LINK=NULL;
}
else
cout<<”Linked List sudah ada””<<endl;
}
Atang Susila T Informatika UNPAM
3. Pembuatan Simpul Awal(2)
Ilustrasi :
void INSERT_KANAN(void)
{
if(LAST!=NULL)
{
LAST->LINK=P;
LAST=P;
P->LINK=NULL;
}
else
cout<<”Linked List belum ada”;
}
Atang Susila T Informatika UNPAM
4. Insert Kanan (2)
Ilustrasi :
LAST->LINK=P atau
FIRST->LINK=P
Atang Susila T Informatika UNPAM
4. Insert Kanan (3)
LAST=P atau
LAST=FIRST->LINK
P->LINK=NULL atau
LAST->LINK=NULL atau
FIRST->LINK->LINK=NULL
Fungsi :
void INSERT_KIRI(void)
{
if(FIRST!=NULL)
{
P->LINK=FIRST;
FIRST=P;
}
else
cout<<”Linked List belum ada”;
}
Atang Susila T Informatika UNPAM
5. Insert Kiri (2)
Sudah ada
linked list
P->LINK=FIRST
atau
P->LINK=LAST
FIRST=P
sudah ada sebuah linked list akan dihapus simpul terakhir menjadi :
Caranya : Fungsi :
Q = FIRST; void DELETE_KANAN(void)
while(Q ->LINK != LAST) {
Q = Q -> LINK; free(LAST);
LAST = Q;
LAST -> LINK = NULL;
}
free(LAST)
LAST = Q
void DELETE_KIRI(void)
{
Q = FIRST;
FIRST = Q -> LINK;
free(Q);
}
Q = FIRST
free(Q)
Caranya : Fungsi :
R = Q -> LINK
free(R)
Tampilan menu :
LIN. SINGLY LINKED LIST
==========================
1.INSERT DATA
2.HAPUS DATA
3.CETAK DATA
4.EXIT
Pilihan (1 – 4) :
Atang Susila T Informatika UNPAM