Upload
tyson
View
34
Download
0
Embed Size (px)
DESCRIPTION
Algoritma dan Struktur Data. Pertemuan 9 Circular Linked List. A. C. B. Struktur Circular Linked List. Node (elemen) circular linked list saling berkait melalui pointer. Bagian next sebuah node menunjuk alamat node selanjutnya pList : pointer yang menunjuk salah satu node pada list. - PowerPoint PPT Presentation
Citation preview
Algoritma dan Struktur Data
Pertemuan 9
Circular Linked List
Struktur Circular Linked List
Node (elemen) circular linked list saling berkait melalui pointer. Bagian next sebuah node menunjuk alamat node selanjutnya
pList: pointer yang menunjuk salah satu node pada list
ApList
B C
Struktur Circular Linked List
Node terakhir menunjuk node pertama Setiap node terdiri atas
Isi data Next, yaitu pointer ke node selanjutnya
pada list
ApList
B C
Struktur Sebuah Node
struct node { //bagian data tipedata data 1; tipedata data 2; … tipedata data n;
//pointer ke node selanjutnya struct node *next; };typedef struct node node;
Deklarasi pList
Sebelum membuat circular linked list, perlu dideklarasikan dan diinisialisasikan pList, yaitu pointer yang menunjuk salah satu node dari circular linked list
node *pList = NULL;
Operasi dasar linked list
1. Menambah sebuah node.
2. Menghapus sebuah node.
3. Mencari sebuah node.
4. List tranversal
Menambahkan node pada linked list
Terdapat empat tahap untuk menambah node linked list: • Membuat node baru.• Mendapatkan node yang terletak sebelum node baru
disisipkan (pPre)• Atur next node baru agar menunjuk node sesudah posisi
penyisipan.• Atur next pPre agar menunjuk node baru.
Nilai (pPre) dapat berisi : • it can contain the address of a node (i.e. you are adding
somewhere after the first node – in the middle or at the end)• it can be NULL i.e. you are adding to an empty list
Menambahkan node ke list kosong
Before: Code: pNew -> next = pNew;
pList = pNew;// point list to first
node
After:
39pNew
pList
pPre
39pNew
pList
pPre
Menambahkan node di tengah list
Before: Code pNew -> next = pPre -> next;
pPre -> next = pNew;
After:
64pNew
pPre
55 124
64pNew
pPre
55 124
Latihan : bagaimana menyisipkan node sebelum pList?
Before: Code ?
After ?
39pNew
pList
pPre
75 124
Kode untuk menambah data ke linked list
• Untuk menambah data pada linked list, harus diketahui pList, pointer yang menunjuk node sebelum tempat penyisipan (pPre) dan data yang akan disisipkan (item).
//insert a node into a linked list node *pNew; pNew = (node *) malloc(sizeof(node)); pNew -> data = item; if (pPre == NULL){ //add an empty list
pNew -> next = pNew; pList = pNew; } else { //add in the middle or at the end pNew -> next = pPre -> next; pPre -> next = pNew; }
Menghapus node dari linked list
• Untuk menghapus sebuah node:
– Cari node yang akan dihapus (pCur) dan node pendahulunya (pPre).
– Ubah pPre->next agar menunjuk pCur->next.
– Hapus pCur menggunakan fungsi free
Menghapus node pertama dari linked list
Before: Code:
pPre -> next = pCur->next;
pList = pList->next;
free(pCur);
After:
pList
pPre
75 124
pCur
pList
pPre
Recycled 124
pCur
Menghapus node dari linked list – kasus umum
Before: Code:
pPre -> next = pCur -> next;
free(pCur);
After:
75 12496
pPre pCur
75 124Recycled
pPre pCur
Kode untuk menghapus node dari linked list
• Untuk menghapus node dari linked list, harus diketahui pList, node yang akan dihapus (pCur), serta pendahulunya (pPre)
//delete a node from a linked list
if (pPre == pCur)
//list satu satu node
pList = NULL;
else if (pCur == pList)
//menghapus node pertama
pPre -> next = pCur -> next;
pList = pList->next;
Else
pPre -> next = pCur -> next;
free(pCur).
Mencari node yang mengandung data tertentu dari linked list
• Operasi insert dan delete membutuhkan pencarian pada list untuk menentukan posisi penyisipan atau pointer yang menunjuk data yang akan dihapus
//search the nodes in a linked listpPre = pList;pCur = pList;//search until the target value is found or the end of the list is reachedDo while (pCur->next != pList && pCur -> data != target) { pPre = pCur; pCur = pCur -> next;}//determine if the target is found or ran off the end of the listif (pCur->data == target) found = 1;else found = 0;
Traversing a Linked List
• mengunjungi semua node yang ada pada list dari head sampai node terakhir
//traverse a linked list
node *pWalker;
pWalker = pList;
printf(“List contains:\n”);
while (pWalker->next != pList){
printf(“%d ”, pWalker -> data);
pWalker = pWalker -> next;
}
printf(“%d ”, pWalker -> data);