1 / 17

Algoritma dan Struktur Data

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.

tyson
Download Presentation

Algoritma dan Struktur Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algoritma dan Struktur Data Pertemuan 9 Circular Linked List

  2. 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 pList

  3. A C B Struktur Circular Linked List • Node terakhir menunjuk node pertama • Setiap node terdiri atas • Isi data • Next, yaitu pointer ke node selanjutnya pada list pList

  4. 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;

  5. 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;

  6. Operasi dasar linked list • Menambah sebuah node. • Menghapus sebuah node. • Mencari sebuah node. • List tranversal

  7. 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

  8. pNew 39 pList pPre pNew 39 pList pPre Menambahkan node ke list kosong Before: Code:pNew -> next = pNew; pList = pNew;// point list to first node After:

  9. pNew 64 55 124 pPre Menambahkan node di tengah list Before: CodepNew -> next = pPre -> next; pPre -> next = pNew; After: pNew 64 55 124 pPre

  10. pNew 39 pList 75 124 pPre Latihan : bagaimana menyisipkan node sebelum pList? Before: Code ? After ?

  11. 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; }

  12. 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

  13. pList 75 124 pCur pPre Menghapus node pertama dari linked list Before: Code: pPre -> next = pCur->next; pList = pList->next; free(pCur); After: pList Recycled 124 pCur pPre

  14. 75 96 124 pCur pPre Recycled 75 124 pCur pPre Menghapus node dari linked list – kasus umum Before: Code: pPre -> next = pCur -> next; free(pCur); After:

  15. 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).

  16. 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 list pPre = pList; pCur = pList; //search until the target value is found or the end of the list is reached Do 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 list if (pCur->data == target) found = 1; else found = 0;

  17. 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);

More Related