Dynamic Array and Linked List Adam M.B.
DYNAMIC ARRAY
DEFINITION AND DECLARATION DEFINITION AND DECLARATION
Description Array which can be managed dynamically in memory (its size). To order the place in memory, it uses POINTER.
Declaration Type Pengenal = ↑Simpul Simpul = TipeData Atau NamaVariabel : ↑TipeData
Example (1) Kamus: Bilangan : ↑integer Nama : ↑string
Example (2) Kamus: Type Point = ↑Data Data = Record < Nim : integer, Nama : string, Nilai : real > EndRecord DataMhs : point
ALLOC AND DEALLOC
Definition Alloc is statement that can be used to order place in memory while program is running. Dealloc is statement that can be used to delete place which had been ordered while program is running.
Ilustration in Memory Alloc(DataMhs) DataMhs Still Empty Nil
OPERATION
Case Kamus: Type Point = ↑Data Data = Record < NamaMhs : string, Jurusan : string > EndRecord T1,T2: point
Copy Pointer (Cont’d) Alloc(T1) T1 Alloc(T2) T2
Copy Pointer (Cont’d) T1 T2 T1.NamaMhs ‘Adam’ T1.Jurusan ‘Teknik Informatika’ T1 T2
Copy Pointer (Cont’d) T1 T2 T1 T2
Copy Value T1 T2 T1 T2
Delete the Pointer Dealloc(T1)
LINKED LIST
DEFINITION
Description Data structure that is prepared sequentially, dynamically, and infinite. Linked list is connected by pointer.
Array VS Linked List ARRAY LINKED LIST Static Dynamic Insert and Delete are finite Insert and Delete are infinite Random Access Sequential Access Delete element is only delete value Delete element is truly delete space
Types of Linked List Single Linked List Double Linked List Circular Linked List
DECLARATION (Single Linked List)
Connection Field (Next) Description Linked list that its node consists of one link/pointer that refers to another node. Ilustration of node: Info Field (Info) Connection Field (Next)
Declaration Kamus: Type NamaPointer = ↑Simpul Simpul = Record < InfoField : TipeData, ConnectionField : NamaPointer > EndRecord NamaVarPointer : NamaPointer
Example Kamus: Type Point = ↑Simpul Simpul = Record < Info : integer, Next : Point > EndRecord Awal,Akhir : Point
OPERATION
Creation Searching Insertion Sorting Delete Destroy Traversal
Creation Pointer (awal and akhir) is given nil as their value. awal awal nil akhir nil
Front Insertion If list is empty (awal = nil). alloc(baru) baru.next nil baru.info 1 baru 1 akhir awal awal baru baru 1 akhir baru
Front Insertion (cont’d) If list isn’t empty (awal ≠ nil). For example, there is list that has two nodes: akhir awal 2 3 One node will be inserted in front of list: alloc(baru) 1 baru baru.info 1
Front Insertion (cont’d) New node will be inserted before the node that was refered by awal. awal 2 akhir 3 baru↑.next awal baru 1
Front Insertion (cont’d) After new node was inserted, move awal to new node. 2 3 baru 1 akhir awal awal baru
Front Insertion (cont’d) The last result for front insertion if linked list wasn’t empty: awal 2 3 akhir baru 1
Procedure SisipDepanSingle(Input elemen : tipedata, I/O awal, akhir : nama_pointer) {I.S. : data yang akan disisipkan (elemen), pointer penunjuk awal dan pointer penunjuk akhir sudah terdifinisi} {F.S. : menghasilkan satu simpul yang disisipkan di depan pada single linked list} Kamus : baru : nama_pointer Algoritma : alloc(baru) baru↑.info elemen If (awal = nil) Then baru↑.next nil akhir baru Else baru↑.next awal EndIf awal baru EndProcedure
Back Insertion If list is empty (awal = nil) the process is same as front insertion if linked list is empty.
Back Insertion (cont’d) If list isn’t empty (awal ≠ nil). For example, there is list that has two nodes: awal 3 2 akhir New node that will be inserted: alloc(baru) baru.next nil baru 1 baru.info 1
Back Insertion (cont’d) New node will be inserted after the node that was refered by akhir. akhir awal 3 2 akhir.next baru baru 1
Back Insertion (cont’d) Move akhir to new node. awal 3 2 baru 1 akhir akhir baru
Back Insertion (cont’d) The last result for back insertion if linked list wasn’t empty: awal 3 2 akhir 1 baru
Procedure SisipBelakangSingle(Input elemen : tipedata, I/O awal, akhir : nama_pointer) {I.S. : data yang akan disisipkan (elemen), pointer penunjuk awal dan pointer penunjuk akhir sudah terdifinisi} {F.S. : menghasilkan satu simpul yang disisipkan di belakang pada single linked list} Kamus : baru : nama_pointer Algoritma : alloc(baru) baru↑.info elemen baru↑.next nil If (awal = nil) Then awal baru Else akhir↑.next baru EndIf akhir baru EndProcedure
Middle Insertion If list is empty (awal = nil) the process is same as front insertion if linked list is empty.
Middle Insertion (cont’d) If list isn’t empty (awal ≠ nil). awal 2 5 4 akhir 3 New node that will be inserted after 4: alloc(baru) baru 1 baru.info 1
Middle Insertion (cont’d) Search node 4 using sequential search and bantu pointer. baru 1 awal 2 5 4 akhir 3 bantu bantu
Middle Insertion (cont’d) Connect the connection field from new node to the neighbour node of node that was refered by bantu. baru 1 awal 2 5 4 akhir 3 bantu baru.next bantu↑.next
Middle Insertion (cont’d) After new node was connected with node 4 then refer the connection field of node that was refered by bantu to new node. bantu awal 2 baru 1 4 akhir 3 5 bantu.next baru
Middle Insertion (cont’d) The last result for middle insertion if linked list wasn’t empty: awal 4 3 bantu 2 akhir 5 1 baru
Procedure SisipTengahSingle(Input elemen : tipedata, I/O awal, akhir : nama_pointer) {I.S. : data yang akan disisipkan (elemen), pointer penunjuk awal dan pointer penunjuk akhir sudah terdifinisi} {F.S. : menghasilkan satu simpul yang disisipkan di tengah pada single linked list} Kamus : baru,bantu : nama_pointer ketemu : boolean datasisip : tipedata Algoritma : If (awal = nil) Then alloc(baru) baru↑.info elemen baru↑.next nil
awal baru akhir baru Else Input(datasisip) bantu awal ketemu false While (not ketemu and bantu ≠ nil) do If (datasisip = bantu↑.info) Then ketemu true bantu bantu↑.next EndIf EndWhile
If (ketemu) Then alloc(baru) baru↑ If (ketemu) Then alloc(baru) baru↑.info elemen If (bantu = akhir) sisip_belakang_single(elemen,awal,akhir) Else baru↑.next bantu↑.next bantu↑.next baru EndIf Output(“Data yang akan disisipkan tidak ada”); EndProcedure
Front Deletion Delete one node in beggining of linked list if linked list has only one node (awal = akhir). awal akhir 1
Front Deletion (cont’d) If deletion happens in linked list with one node then linked list will be empty. 1 awal akhir phapus phapus awal elemen phapus .info elemen awal akhir awal nil akhir nil dealloc(phapus) 1 phapus
Front Deletion (cont’d) If linked list has more than one node (awal ≠ akhir). For example, linked list has two nodes. awal 2 3 akhir
Front Deletion (cont’d) awal 2 3 akhir phapus phapus awal elemen elemen phapus.info awal awal.next awal 2 3 phapus akhir dealloc(phapus)
Front Deletion (cont’d) The last result for front deletion if linked list has more than one node: awal 3 akhir 2 phapus
Procedure HapusDepanSingle(Output elemen : tipedata, I/O awal, akhir : nama_pointer) {I.S. : pointer penunjuk awal dan pointer penunjuk akhir sudah terdifinisi} {F.S. : menghasilkan single linked list yang sudah dihapus satu simpul di depan} Kamus : phapus : nama_pointer Algoritma : phapus awal elemen baru↑.info If (awal = akhir) Then awal nil akhir nil Else awal awal ↑.next EndIf dealloc(phapus) EndProcedure
Back Deletion Delete one node in back of linked list if linked list has only one node (awal = akhir). This process is same as front deletion if linked list has only one node.
Back Deletion (cont’d) If linked list has more than one node (awal ≠ akhir). For example, linked list has three nodes. awal 1 3 2 akhir
Back Deletion (cont’d) awal 1 3 akhir 2 phapus elemen phapus awal elemen akhir.info phapus phapus.next akhir phapus awal 1 3 2 akhir phapus phapus phapus phapus.next
Back Deletion (cont’d) awal 1 3 phapus 2 akhir akhir.next nil dealloc(phapus)
Back Deletion (cont’d) The last result for back deletion if linked list has more than one node: awal 1 2 akhir phapus phapus 3
Procedure HapusBelakangSingle(Output elemen : tipedata, I/O awal, akhir : nama_pointer) {I.S. : pointer penunjuk awal dan pointer penunjuk akhir sudah terdifinisi} {F.S. : menghasilkan single linked list yang sudah dihapus satu simpul di belakang} Kamus : phapus : nama_pointer Algoritma : phapus awal elemen baru↑.info If (awal = akhir) Then awal nil akhir nil
Else while (phapus↑. next ≠ akhir) do phapus phapus↑ Else while (phapus↑.next ≠ akhir) do phapus phapus↑.next endwhile akhir phapus akhir↑.next nil EndIf dealloc(phapus) EndProcedure
Middle Deletion Delete one node in middle of linked list if linked list has only one node (awal = akhir). This process is same as front deletion if linked list has only one node.
Middle Deletion (cont’d) If linked list has more than one node (awal ≠ akhir). For example, linked list has four nodes and user want to delete third node. awal 2 5 4 akhir 3
Middle Deletion (cont’d) Search the third node start from the first node. If node is found then save the info of this node to the variable elemen. posisihapus=3 posisihapus=2 posisihapus=1 awal 2 5 4 akhir 3 phapus phapus elemen elemenphapus.info
Middle Deletion (cont’d) Because the connection field of previous node must be connected to the next node of node that will be deleted so we need pointer bantu that refer the node that will be deleted. awal 2 5 4 phapus akhir 3 posisihapus=3 bantu
Middle Deletion (cont’d) Connect the connection field of the node that was referred by pointer bantu to the neighbour node. Delete the node that was referred by pointer phapus. awal 2 5 4 phapus akhir 3 posisihapus=3 bantu bantu.next phapus.next dealloc(phapus)
Middle Deletion (cont’d) The last result for middle deletion if linked list has more than one node: posisihapus=3 posisihapus=2 posisihapus=1 phapus awal 4 3 bantu phapus 4 3 5 akhir awal akhir 2 5
Traversal Operation that visiting all nodes in linked list start from the first node until last node. awal 2 5 4 akhir 3 For example, shown the process to output data: Place one pointer bantu in the first node awal 2 5 4 akhir 3 bantu
Traversal The value in info field will be output to screen from the node that was referred by bantu then pointer bantu will move to the next node until all nodes were visited (bantu = nil). awal 2 5 4 akhir 3 bantu bantu bantu bantu Screen Output: 3 4 2 5
GRACIAS THANK YOU Copyright © Adam Mukharil Bachtiar 2012 Contact Person: Adam Mukharil Bachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor. 112-114 Bandung 40132 Email: adfbipotter@gmail.com Blog: http://adfbipotter.wordpress.com Copyright © Adam Mukharil Bachtiar 2012