Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Kuliah 3 Struktur Data Linked List dan Double Linked List.

Presentasi serupa


Presentasi berjudul: "Kuliah 3 Struktur Data Linked List dan Double Linked List."— Transcript presentasi:

1 Kuliah 3 Struktur Data Linked List dan Double Linked List

2 © 2005 Pearson Addison- Wesley. All rights reserved 4-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

3 © 2005 Pearson Addison-Wesley. All rights reserved4-3 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

4 © 2005 Pearson Addison-Wesley. All rights reserved4-4 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

5 © 2005 Pearson Addison- Wesley. All rights reserved 4-5 Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell

6 © 2005 Pearson Addison- Wesley. All rights reserved 4-6 Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next

7 © 2005 Pearson Addison- Wesley. All rights reserved 4-7 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

8 © 2005 Pearson Addison-Wesley. All rights reserved4-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

9 LATIHAN

10 SOAL Buatlah fungsi untuk : 1.melakukan pencarian berdasarkan key tertentu 2.melakukan perhitungan berapa banyak data tertentu pada list tersebut 3.melakukan penghapusan key tertentu dalam list 4.melakukan penghapusan node di akhir list 5.melakukan update terhadap nilai suatu node 6.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->datadata>dataBaru) //call insertAwal else{ bantu=awal; while(bantu->next->datanext; if(bantu!=NULL) { hold=bantu->next; bantu->next=baru; baru->next=hold; }}}}

17 © 2005 Pearson Addison-Wesley. All rights reserved4-17 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

18 © 2005 Pearson Addison-Wesley. All rights reserved4-18 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

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

21 Node data (cont.) template struct NodeType { ItemType info; NodeType * next; NodeType * 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 4. location->back = newNode;

25 Deleting from a Doubly Linked List Be careful about the end cases!!

26 © 2005 Pearson Addison- Wesley. All rights reserved 4-26 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

27 © 2005 Pearson Addison- Wesley. All rights reserved 4-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


Download ppt "Kuliah 3 Struktur Data Linked List dan Double Linked List."

Presentasi serupa


Iklan oleh Google