Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Dynamic Array and Linked List

Presentasi serupa


Presentasi berjudul: "Dynamic Array and Linked List"— Transcript presentasi:

1 Dynamic Array and Linked List
Adam M.B.

2  DYNAMIC ARRAY

3 DEFINITION AND DECLARATION
 DEFINITION AND DECLARATION

4 Description Array which can be managed dynamically in memory (its size). To order the place in memory, it uses POINTER.

5 Declaration Type Pengenal = ↑Simpul Simpul = TipeData Atau
NamaVariabel : ↑TipeData

6 Example (1) Kamus: Bilangan : ↑integer Nama : ↑string

7 Example (2) Kamus: Type Point = ↑Data Data = Record
< Nim : integer, Nama : string, Nilai : real > EndRecord DataMhs : point

8  ALLOC AND DEALLOC

9 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.

10 Ilustration in Memory Alloc(DataMhs) DataMhs Still Empty Nil

11  OPERATION

12 Case Kamus: Type Point = ↑Data Data = Record < NamaMhs : string,
Jurusan : string > EndRecord T1,T2: point

13 Copy Pointer (Cont’d) Alloc(T1) T1 Alloc(T2) T2

14 Copy Pointer (Cont’d) T1 T2 T1.NamaMhs  ‘Adam’
T1.Jurusan  ‘Teknik Informatika’ T1 T2

15 Copy Pointer (Cont’d) T1 T2  T1 T2

16 Copy Value T1 T2  T1 T2

17 Delete the Pointer Dealloc(T1)

18  LINKED LIST

19  DEFINITION

20 Description Data structure that is prepared sequentially, dynamically, and infinite. Linked list is connected by pointer.

21 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

22 Types of Linked List Single Linked List Double Linked List
Circular Linked List

23  DECLARATION (Single Linked List)

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

25 Declaration Kamus: Type NamaPointer = ↑Simpul Simpul = Record
< InfoField : TipeData, ConnectionField : NamaPointer > EndRecord NamaVarPointer : NamaPointer

26 Example Kamus: Type Point = ↑Simpul Simpul = Record
< Info : integer, Next : Point > EndRecord Awal,Akhir : Point

27  OPERATION

28 Creation Searching Insertion Sorting Delete Destroy Traversal

29 Creation Pointer (awal and akhir) is given nil as their value. awal
awal  nil akhir  nil

30 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

31 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

32 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

33 Front Insertion (cont’d)
After new node was inserted, move awal to new node. 2 3 baru 1 akhir awal awal  baru

34 Front Insertion (cont’d)
The last result for front insertion if linked list wasn’t empty: awal 2 3 akhir baru 1

35 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

36 Back Insertion If list is empty (awal = nil)  the process is same as front insertion if linked list is empty.

37 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

38 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

39 Back Insertion (cont’d)
Move akhir to new node. awal 3 2 baru 1 akhir akhir  baru

40 Back Insertion (cont’d)
The last result for back insertion if linked list wasn’t empty: awal 3 2 akhir 1 baru

41 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

42 Middle Insertion If list is empty (awal = nil)  the process is same as front insertion if linked list is empty.

43 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

44 Middle Insertion (cont’d)
Search node 4 using sequential search and bantu pointer. baru 1 awal 2 5 4 akhir 3 bantu bantu

45 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

46 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

47 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

48 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

49 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

50 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

51 Front Deletion Delete one node in beggining of linked list if linked list has only one node (awal = akhir). awal akhir 1

52 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

53 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

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

55 Front Deletion (cont’d)
The last result for front deletion if linked list has more than one node: awal 3 akhir 2 phapus

56 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

57 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.

58 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

59 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

60 Back Deletion (cont’d)
awal 1 3 phapus 2 akhir akhir.next  nil dealloc(phapus)

61 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

62 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

63 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

64 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.

65 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

66 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 elemenphapus.info

67 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

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

69 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

70 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

71 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

72 GRACIAS THANK YOU Copyright © Adam Mukharil Bachtiar 2012
Contact Person: Adam Mukharil Bachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor Bandung 40132 Blog: Copyright © Adam Mukharil Bachtiar 2012


Download ppt "Dynamic Array and Linked List"

Presentasi serupa


Iklan oleh Google