Anda di halaman 1dari 67

Double Linked List

Entin Martiana

Operasi Double Linked List Membangun Double Linked List Membaca list, dalam dua arah. Mencari simpul tertentu Menghapus simpul tertentu Menyisipkan sebagai simpul pertama Menyisipkan simpul di tengah

Double Linked List

head

tail

Deklarasi
struct simpul { char nama[25]; int nrp; struct simpul *before; data struct simpul *next; pointer yg menunjuk simpul }; sebelumnya pointer yg menunjuk simpul
berikutnya

simpul

nama nrp

before next

struct simpul *baru;

Membangun Linked List


Apa yang harus dilakukan? 1. Deklarasi 2. Memory allocation 3. Mengisi data 4. Menyiapkan untuk dihubungkan dengan data baru berikutnya

Bagaimana?
baru

1. 2. 3. 4. 5.

struct simpul *baru; baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(pertama) { baru->before=NULL;

nama1 nrp1 before next

baru

nama1 nrp1
NULL

before next

Bagaimana?
baru

1. 2. 3. 4. 5.

struct simpul *baru; baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(pertama) { baru->before=NULL; baru->next=NULL;
baru

nama1 nrp1 before next

nama1 nrp1
NULL

before next
NULL

Bagaimana?
baru

1. 2. 3. 4. 5.

struct simpul *baru; baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(pertama) { baru->before=NULL; baru->next=NULL; head=baru;
head baru

nama1 nrp1 before next

nama1 nrp1
NULL

before next
NULL

Bagaimana?
baru 1. 2. 3. 4. 5. struct simpul *baru; baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(pertama) { baru->before=NULL; baru->next=NULL; head = baru; tail = baru; } head baru tail

nama1 nrp1 before next

nama1 nrp1 before next

NULL

NULL

Selanjutnya
1. 2. 3. 4. baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(bukanpertama) { baru->next=NULL;
baru

nama2 nrp2 before next

head

tail

baru

nama1

nama2

nrp1
before
NULL

nrp2
before next
NULL

next

NULL

Selanjutnya
1. 2. 3. 4. baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(bukanpertama) { baru->next=NULL; tail->next=baru;
baru

nama2 nrp2 before next

head

tail

baru

nama1

nama2

nrp1
before
NULL

nrp2
before next
NULL

next

Selanjutnya
1. 2. 3. 4. baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(bukanpertama) { baru->next=NULL; tail->next=baru; baru->before=tail;
tail baru baru

nama2 nrp2 before next

head

nama1

nama2

nrp1
before
NULL

nrp2
before next
NULL

next

Selanjutnya
1. 2. 3. 4. baru=(struct simpul*)malloc(sizeof(struct simpul)); printf("Nama :");scanf("%s",&baru->nama); printf("NRP :");scanf("%d",&baru->nrp); if(j<>0) { baru->next=NULL; tail->next=baru; baru->before=tail; tail=baru; tail }
head baru

nama2 nrp2 before next

nama1

nama2

nrp1
before
NULL

nrp2
before next
NULL

next

Sampai iterasi keempat

head

tail

nama1 nrp1 before


NULL

nama2
nrp2 before next

nama3 nrp3 before next

nama4 nrp4 before next

next

NULL

Membaca (FIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
bacaFIFO

next

bacaFIFO = head;

Membaca (FIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
bacaFIFO

next

while (bacaFIFO!=NULL) // fungsi menampilkan

bacaFIFO = bacaFIFO -> next;

Membaca (FIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next

next
bacaFIFO

while (bacaFIFO!=NULL) // fungsi menampilkan

bacaFIFO = bacaFIFO -> next;

Membaca (FIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


bacaFIFO

nama4 nrp4 before next


NULL

next

next

while (bacaFIFO!=NULL) // fungsi menampilkan

bacaFIFO = bacaFIFO -> next;

Membaca (FIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL bacaFIFO

next

next

while (bacaFIFO<>NULL) // fungsi menampilkan

bacaFIFO = bacaFIFO -> next;

Membaca (LIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL bacaLIFO

next

next

bacaLIFO = tail;

Membaca (LIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL bacaLIFO

next

next

while (bacaLIFO<>NULL) // fungsi menampilkan

bacaLIFO = bacaLIFO -> before;

Membaca (LIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


bacaLIFO

nama4 nrp4 before next


NULL

next

next

while (bacaLIFO<>NULL) // fungsi menampilkan

bacaLIFO = bacaLIFO -> before;

Membaca (LIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next

next
bacaLIFO

while (bacaLIFO<>NULL) // fungsi menampilkan

bacaLIFO = bacaLIFO -> before;

Membaca (LIFO)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
bacaLIFO

next

while (bacaLIFO<>NULL) // fungsi menampilkan

bacaLIFO = bacaLIFO -> before;

Mencari Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari

next

cari = head;

Mencari Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari

next

while (cari->nama!=nama3) cari = cari -> next;

Mencari Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next

next
cari

while (cari->nama!=nama3) cari = cari -> next;

Mencari Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL

next

next

while (cari->nama!=nama3) cari = cari -> next;

Menghapus Simpul Tertentu (Simpul Depan)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari

next

cari = head;

Menghapus Simpul Tertentu (Simpul Depan)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari

next

cari = head; if(cari->nama==nama1)

head=head->next;

Menghapus Simpul Tertentu (Simpul Depan)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari NULL

next

cari = head; if(cari->nama==nama)

head=head->next;
head->before=NULL;

Menghapus Simpul Tertentu (Simpul Depan)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari NULL

next

cari = head; if(cari->nama==nama)

head=head->next;
head->before=NULL; free(cari);

Menghapus Simpul Tertentu (Simpul Depan)

head

tail

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
NULL

cari = head; if(cari->nama==nama)

head=head->next;
head->before=NULL; free(cari);

Menghapus Simpul Tertentu (Simpul Akhir)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next

next

cari

cari=tail;

Menghapus Simpul Tertentu (Simpul Akhir)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next

next

cari

tail=tail->before;

Menghapus Simpul Tertentu (Simpul Akhir)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


NULL

nama4 nrp4 before next


cari

next

next

tail=tail->before; tail->next=NULL;

Menghapus Simpul Tertentu (Simpul Akhir)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


NULL

nama4 nrp4 before next


cari

next

next

tail=tail->before; tail->next=NULL;

free(cari);

Menghapus Simpul Tertentu (Simpul Akhir)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


NULL

next

next

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari

next

cari=head;

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next
cari

next

while (cari->nama!=nama3) cari = cari -> next;

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next

nama4 nrp4 before next


NULL

next

next
cari

while (cari->nama!=nama3) cari = cari -> next;

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL

next

next

while (cari->nama!=nama3) cari = cari -> next;

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL

next

next

cari->before->next=cari->next;

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL

next

next

cari->next->before=cari->before;

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL

next

next

free(cari);

Menghapus Simpul Tertentu (Di Tengah)

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama4 nrp4 before next


NULL

next

next

atau

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL stl

next

next
sbl

while (cari->nama!=nama3) cari = cari -> next;

sbl=cari->before;
stl=cari->next;

Menghapus Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL stl

next

next
sbl

sbl->next=stl;

Menghapus Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama3 nrp3 before next


cari

nama4 nrp4 before next


NULL stl

next

next
sbl

sbl->next=stl; stl->before=sbl;

Menghapus Simpul Tertentu

head

tail

nama1 nrp1 before


NULL

nama2 nrp2 before

nama4 nrp4 before next


NULL

next

next

cari->before->next = cari->next; cari->next->before = cari->before;

free(cari);

Menyisipkan sebagai simpul pertama

head

tail

nama1 nrp1 before


NULL

nama2
nrp2 before next
sisip

nama3 nrp3 before next

nama4 nrp4 before next

next

NULL

namax nrpx before next

Menyisipkan sebagai simpul pertama

head

tail

nama1 nrp1 before


NULL

nama2
nrp2 before next
sisip

nama3 nrp3 before next

nama4 nrp4 before next

next

NULL

namax nrpx before


NULL

sisip->before=NULL;

next

Menyisipkan sebagai simpul pertama

head

tail

nama1 nrp1 before


NULL

nama2
nrp2 before next
sisip

nama3 nrp3 before next

nama4 nrp4 before next

next

NULL

namax nrpx before


NULL

sisip->next=head;

next

Menyisipkan sebagai simpul pertama

head

tail

nama1 nrp1 before next


sisip

nama2
nrp2 before next

nama3 nrp3 before next

nama4 nrp4 before next

NULL

namax nrpx before


NULL

head->before=sisip;

next

Menyisipkan sebagai simpul pertama

tail

nama1 nrp1 before next


sisip head

nama2
nrp2 before next

nama3 nrp3 before next

nama4 nrp4 before next

NULL

namax nrpx before


NULL

head=sisip;

next

Menyisipkan stl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL sisip

next

namax nrpx before next

Menyisipkan stl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


stl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL sisip

next

stl=head; while(stl->nama!=nama3) stl=stl->next;

namax nrpx before next

Menyisipkan stl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


stl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL sisip

next

sisip->before=stl;

namax nrpx before next

Menyisipkan stl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


stl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL sisip

next

sisip->before=stl; sisip->next=stl->next;

namax nrpx before next

Menyisipkan stl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


stl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL sisip

next

sisip->before=stl; sisip->next=stl->next; stl->next->before=sisip;

namax nrpx before next

Menyisipkan stl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


stl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL sisip

next

sisip->before=stl; sisip->next=stl->next; stl->next->before=sisip; stl->next=sisip;

namax nrpx before next

Menyisipkan sbl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


sisip

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL

next

namax nrpx before next

Menyisipkan sbl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


sisip sbl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL

next

sbl=head; while(sbl->nama!=nama3) sbl=sbl->next;

namax nrpx before next

Menyisipkan sbl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


sisip sbl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL

next

sisip->next=sbl;

namax nrpx before next

Menyisipkan sbl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


sisip sbl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL

next

sisip->next=sbl; sisip->before=sbl->before;

namax nrpx before next

Menyisipkan sbl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


sisip sbl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL

next

sisip->next=sbl; sisip->before=sbl->before; sbl->before->next=sisip;

namax nrpx before next

Menyisipkan sbl. simpul tertentu


head

tail

nama1 nrp1

nama2

nama3 nrp3 before next


sisip sbl

nama4 nrp4

nrp2
before next

before
NULL

before next
NULL

next

sisip->next=sbl; sisip->before=sbl->before; sbl->before->next=sisip; sbl->before=sisip;

namax nrpx before next