DIG1G3 Implementasi Struktur Data

advertisement
DIG1G3
Implementasi Struktur Data
Program Studi Diploma III Teknik Informatika
Fakultas Ilmu Terapan
Telkom University
Dosen: Cahyana, S.T., M.Kom.
Indra Azimi, S.T., M.T.
Tujuan Pertemuan 9
• Mahasiswa mengetahui operasi yang dapat
dilakukan pada circular singly linked list:
▫ Insert list
▫ Delete
3
Background
• A singly linked list is time consuming if we want
to
▫ move to the end of the list.
• The LIST ADT can also be implemented using a
▫ Circular linked list (singly or doubly)
4
Circular Singly Linked List
• Last node references to the first node
• Every node has a successor
• No node in a circular linked list contains NULL
Head
Other presentations
In a circular linked list with more than one node, as in (c), it is convenient to
make the pointer first point to the last node of the list. Then by using first you
can access both the first and the last node of the list.
For example, first points to the last node and first->link points to the first
node.
6
Circular Linked List - Insert
• Insert Depan
▫ Penambahan node baru akan dikaitan di node paling
depan, namun pada saat pertama kali (data masih
kosong), maka penambahan data dilakukan pada head
nya.
• Insert Belakang
▫ Penambahan data dilakukan di belakang,
namun pada saat pertama kali data langsung
ditunjuk pada head-nya.
Insert Depan
Insert Belakang
Implementasi
• Bagaimana implementasi insert depan dan
belakang?
• Pada dasarnya, struktur data pada circular
singly linked list sama dengan singly linked list
biasa
typedef struct node
{
int info;
node *link;
} nodeType;
nodeType *head;
Implementasi cont’l
• Ketika kosong, head menunjuk ke NULL
• Masuk node baru:
Head = baru;
head->next = head;
Implementasi – Insert Depan
• Jika linked list tidak kosong
• Gunakan pointer bantuan:
nodeType *bantu;
bantu = head;
while(bantu->next!=head){
bantu = bantu->next;
}
baru->next = head;
head = baru;
bantu->next = head;
Delete Depan
• Gunakan pointer bantuan
Delete Depan Cont’l
• Jika linked list tidak kosong dan memiliki lebih
dari satu node (size >1)
hapus = head;
if(head->next != head){
bantu = head;
while(bantu->next!=head){
bantu=bantu->next;
}
head = head->next;
delete hapus;
bantu->next = head;
14
Reference
• Lecture slide, Linked List Variants, CS2014
Algoritma & Struktur Data
• DS Malik, Data Structures Using C++, 2nd
Edition
• Antonius Rachmat C, S.Kom, STRUKTUR DATA
(7) - single linked list circular, Lecture slide
Download