Circular Linked List - m1perpustakaanmateri

Download Report

Transcript Circular Linked List - m1perpustakaanmateri

Algoritma dan Struktur Data

Pertemuan 9 Circular Linked List

pList

Struktur Circular Linked List

A B C  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

Struktur Circular Linked List

A B C  Node terakhir menunjuk node pertama  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 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;

pNew 39

pList = pNew;// point list to first node

pList pPre

After:

pNew pList pPre 39

Menambahkan node di tengah list

Before: Code pNew -> next = pPre -> next;

pNew 64

pPre -> next = pNew;

55 124 pPre

After:

pNew 64 55 124 pPre

Latihan : bagaimana menyisipkan node sebelum pList?

Before: Code ?

pNew 39 pList 75 124 pPre

After ?

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 pendahulunya (pPre).

akan dihapus (pCur) – Ubah pPre->next agar menunjuk pCur->next.

– Hapus pCur menggunakan fungsi free dan node

Before:

pList

Menghapus node pertama dari linked list

75 pCur 124

Code: pPre -> next = pCur->next; pList = pList->next; free(pCur);

pPre

After:

pList pPre Recycled pCur 124

pPre

Menghapus node dari linked list – kasus umum

Before: 75 96 124

Code: pPre -> next = pCur -> next; free(pCur);

pCur After: Recycled 75 124 pCur pPre

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

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