Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
1
Linked List dan Double Linked List
Kuliah 3 Struktur Data Linked List dan Double Linked List
2
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
3
© 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
4
© 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
5
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
6
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
7
Deleting a Specified Node from a Linked List
Figure Deleting a node from a linked list Figure Deleting the first node © 2005 Pearson Addison-Wesley. All rights reserved
8
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
9
LATIHAN
10
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
11
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;
12
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;
13
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”);
14
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;
15
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;
16
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; }}}}
17
© 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 A circular linked list © 2005 Pearson Addison-Wesley. All rights reserved
18
© 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 A dummy head node © 2005 Pearson Addison-Wesley. All rights reserved
19
Doubly Linked Lists
20
Node data info: the user's data
next, back: the address of the next and previous node in the list .back .info .next
21
Node data (cont.) template<class ItemType> struct NodeType {
ItemType info; NodeType<ItemType>* next; NodeType<ItemType>* back; };
22
Finding a List Item We no longer need to use prevLocation (we can get the predecessor of a node using its back member)
23
Finding a List Item (cont.)
24
Inserting into a Doubly Linked List
1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location location->back = newNode;
25
Deleting from a Doubly Linked List
Be careful about the end cases!!
26
Doubly Linked Lists Figure (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
27
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
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.