Algoritma dan Struktur Data Pertemuan 7 Linked List
Definitions Linked List Struktur data yang terdiri atas sekumpulan data bertipe sama Memperhatikan urutan Array Struktur data yang terdiri atas sekumpulan data bertipe sama Memperhatikan urutan Apa perbedaannya?
Array vs linked list Banyaknya anggota Banyaknya elemen array ditentukan di awal & jumlahnya tetap Elemen linked list dibuat di memori ketika dibutuhkan (ingat pertemuan 6). Jumlahnya dinamis, dapat bertambah dan berkurang sesuai keperluan Cara mengakses elemen Elemen array diakses lewat indeks Untuk mengakses elemen linked list, harus dilakukan penelusuran elemen list
Struktur linked List phead A B C Node (elemen) linked list saling berkait melalui pointer. Bagian next sebuah node menunjuk alamat node selanjutnya phead: pointer yang menunjuk node pertama
Struktur linked List phead A B C Node terakhir menunjuk NULL Setiap node terdiri atas Isi data Next, yaitu pointer ke node selanjutnya pada list
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 head Sebelum membuat linked list, perlu dideklarasikan dan diinisialisasikan head, yaitu pointer yang menunjuk node pertama dari linked list node *phead = 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 () Atur next node baru agar menunjuk node sesudah posisi penyisipan. Atur next agar menunjuk node baru. Nilai () 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 either to an empty list or at the beginning of the list)
Menambahkan node ke list kosong Before: pnew phead 39 Code: pnew -> next = phead; // set link to NULL phead = pnew;// point list to first node After: pnew 39 phead
Menambahkan node ke awal list Before: pnew 39 Code (same): pnew -> next = phead; // set link to NULL phead = pnew;// point list to first node phead 75 124 After: pnew 39 phead 75 124
Menambahkan node di tengah list Before: pnew 64 Code pnew -> next = -> next; -> next = pnew; 55 124 After: pnew 64 55 124
Menambahkan node akhir list Before: pnew 144 Code pnew -> next = NULL; -> next = pnew; 55 124 After: pnew 144 55 124
Kode untuk menambah data ke linked list Untuk menambah data pada linked list, harus diketahui head pointer (phead), pointer yang menunjuk node sebelum tempat penyisipan () data yang akan disisipkan (item). //insert a node into a linked list struct node *pnew; pnew = (struct node *) malloc(sizeof(struct node)); pnew -> data = item; if ( == NULL){ //add before first logical node or to an empty list pnew -> next = phead; phead = pnew; } else { //add in the middle or at the end pnew -> next = -> next; -> next = pnew; }
Menghapus node dari linked list Untuk menghapus sebuah node: Cari node yang akan dihapus (pcur) dan node pendahulunya (). Ubah ->next agar menunjuk pcur->next. Hapus pcur menggunakan fungsi free
Menghapus node pertama dari linked list Before: phead 75 124 Code: phead = pcur -> next; free(pcur); pcur After: phead Recycled 124 pcur
Menghapus node dari linked list kasus umum Before: 75 96 124 Code: -> next = pcur -> next; free(pcur); pcur After: Recycled 75 124 pcur
Kode untuk menghapus node dari linked list Untuk menghapus node dari linked list, harus diketahui head pointer (phead), node yang akan dihapus (pcur), serta pendahulunya, //delete a node from a linked list if ( == NULL) //deletion is on the first node of the list phead = pcur -> next; else //deleting a node other than the first node of the list -> 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 list = NULL; pcur = phead; //search until the target value is found or the end of the list is reached while (pcur!= NULL && pcur -> data!= target) { = pcur; pcur = pcur -> next; } //determine if the target is found or ran off the end of the list if (pcur!= NULL) 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 Struct node *pwalker; pwalker = phead; printf( List contains:\n ); while (pwalker!= NULL){ } printf( %d, pwalker -> data); pwalker = pwalker -> next;