Linked List dan Double Linked List Kuliah 3 Struktur Data Linked List dan Double Linked List
Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to grow in size as needed Does not require the shifting of items during insertions and deletions © 2005 Pearson Addison-Wesley. All rights reserved
© 2005 Pearson Addison-Wesley. All rights reserved Pointers Figure 4.3 (a) declaring pointer variables; (b) pointing to statically allocating memory; (c) assigning a value; (d) allocating memory dynamically; (e) assigning a value © 2005 Pearson Addison-Wesley. All rights reserved
© 2005 Pearson Addison-Wesley. All rights reserved Pointers Figure 4.3 (f) copying a pointer; (g) allocating memory dynamically and assigning a value; (h) assigning NULL to a pointer variable; (i) deallocating memory © 2005 Pearson Addison-Wesley. All rights reserved
Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell © 2005 Pearson Addison-Wesley. All rights reserved
Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next © 2005 Pearson Addison-Wesley. All rights reserved
Deleting a Specified Node from a Linked List Figure 4.10 Deleting a node from a linked list Figure 4.11 Deleting the first node © 2005 Pearson Addison-Wesley. All rights reserved
Inserting a Node into a Specified Position Figure 4.12 Inserting a new node into a linked list Figure 4.13 Inserting at the beginning of a linked list Figure 4.14 Inserting at the end of a linked list © 2005 Pearson Addison-Wesley. All rights reserved
LATIHAN
SOAL Buatlah fungsi untuk : melakukan pencarian berdasarkan key tertentu melakukan perhitungan berapa banyak data tertentu pada list tersebut melakukan penghapusan key tertentu dalam list melakukan penghapusan node di akhir list melakukan update terhadap nilai suatu node melakukan penambahan data dengan memperhatikan urutan
1. melakukan pencarian berdasarkan key tertentu int cari(int key,node *awal,node *akhir){ node *bantu; bantu = awal; while(bantu->data!=key && bantu!=NULL) { bantu=bantu->next; } if(bantu!=NULL) return 1; else return 0;
2. melakukan perhitungan berapa banyak data tertentu pada list tersebut int count(int key,node *awal,node *akhir){ node *bantu; int jumlah=0; bantu = awal; while(bantu!=NULL) { if(bantu->data==key) jumlah++; bantu=bantu->next; } return jumlah;
3. melakukan penghapusan key tertentu dalam list void hapusKey(int key,node *awal,node *akhir){ node *bantu, *hold; bantu = awal; while(bantu->next->data!=key && bantu!=NULL) { bantu=bantu->next; } if(bantu!=NULL) hold=bantu->next; bantu->next=hold->next; free(hold); else printf(“Data tidak ketemu”);
4. melakukan penghapusan node di akhir list void hapusKey(node *awal,node *akhir){ node *bantu; bantu = awal; while(bantu->next!=akhir) { bantu=bantu->next; } akhir=bantu; free(bantu->next); akhir->next=NULL;
5. melakukan update terhadap nilai suatu node void update(int baru, int lama,node *awal,node *akhir){ node *bantu; bantu = awal; while(bantu->data!=lama && bantu!=NULL) { bantu=bantu->next; } if(bantu!=NULL) bantu->data=baru;
void insertUrut(int dataBaru, node *awal, node *akhir){ node *baru,*bantu, *hold; baru=malloc(sizeof(node)); baru->data=dataBaru; if (awal==NULL){ awal=baru; akhir=baru; awal->next=NULL; } else { if(akhir->data<dataBaru) //call insertAkhir else if(awal->data>dataBaru) //call insertAwal else{ bantu=awal; while(bantu->next->data<dataBaru && bantu!=NULL) bantu=bantu->next; if(bantu!=NULL) { hold=bantu->next; bantu->next=baru; baru->next=hold; }}}}
© 2005 Pearson Addison-Wesley. All rights reserved Circular Linked Lists Last node references the first node Every node has a successor No node in a circular linked list contains NULL Figure 4.25 A circular linked list © 2005 Pearson Addison-Wesley. All rights reserved
© 2005 Pearson Addison-Wesley. All rights reserved Dummy Head Nodes Dummy head node Always present, even when the linked list is empty Insertion and deletion algorithms initialize prev to reference the dummy head node, rather than NULL Figure 4.27 A dummy head node © 2005 Pearson Addison-Wesley. All rights reserved
Doubly Linked Lists
Node data info: the user's data next, back: the address of the next and previous node in the list .back .info .next
Node data (cont.) template<class ItemType> struct NodeType { ItemType info; NodeType<ItemType>* next; NodeType<ItemType>* back; };
Finding a List Item We no longer need to use prevLocation (we can get the predecessor of a node using its back member)
Finding a List Item (cont.)
Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;
Deleting from a Doubly Linked List Be careful about the end cases!!
Doubly Linked Lists Figure 4.29 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node © 2005 Pearson Addison-Wesley. All rights reserved
Summary Recursion can be used to perform operations on a linked list In a circular linked list, the last node points to the first node Dummy head nodes eliminate the special cases for insertion into and deletion from the beginning of a linked list © 2005 Pearson Addison-Wesley. All rights reserved