data structure (double linked list)

Post on 20-Mar-2017

100 Views

Category:

Software

16 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Double Linked List

Adam M.B.

DEFINITION

Linked list that its node consists of two

connection fields (prev and next).

Double Linked List

Info Field (Info)

Right Connection Field (Next)

Left Connection Field (Prev)

DeclarationKamus:

Type

nama_pointer = ↑Simpul

Simpul = Record

< medan_data : tipedata,

prev, next : nama_pointer >

EndRecord

nama_var_pointer : nama_pointer 

Example of DeclarationKamus:

Type

point = ↑Simpul

Simpul = Record

< info : char,

prev, next : point >

EndRecord

awal,akhir : point 

Operation• Creation• Insertion• Delete• Traversal• Searching• Sorting• Destroy

Same with single linked list

CREATION

ProcessPointer awal and akhir is given nil value.

awal akhir

INSERTION

• If list is empty (awal = nil).

Front Insertion

awal

akhir

baru

1

akhir baru

awal baru

baru↑.info 1baru↑.next nilbaru↑.prev nil

alloc(baru)

• If list isn’t empty (awal ≠ nil). For example, there is list that has two nodes:

Front Insertion (cont’d)

awal

2 3

akhir

baru 1alloc(baru)baru↑.info 1baru↑.prev nil

Front Insertion (cont’d)

2 3

baru

1baru↑.next awalawal↑.prev baruawal baru

awal akhir

The last result for front insertion if linked list wasn’t empty:

Front Insertion (cont’d)

2 3

akhir

1

awal

baru

• If list is empty (awal = nil) the

process is same as front insertion if double linked list is empty.

Back Insertion

• If list isn’t empty (awal ≠ nil). For example, there is list that has two nodes:

Back Insertion (cont’d)

awal

2 3

akhir

baru

1alloc(baru)baru↑.info 1baru↑.next nil

New node will be inserted after the node that was refered by akhir.

Back Insertion (cont’d)

baru

awal

2 3

akhir

1baru↑.prev akhirakhir↑.next baruakhir baru

The last result for back insertion if linked list wasn’t empty:

Back Insertion (cont’d)

baru

2 3

1

awal akhir

awal

2 3

akhir

1

• If list is empty (awal = nil) the

process is same as front insertion if linked list is empty.

Middle Insertion

• If list isn’t empty (awal ≠ nil).

Middle Insertion (cont’d)

Node 4 will be inserted before 9:

Awal5 97

Akhir

10

Awal

5 97

Akhir

10

bantu

Middle Insertion (cont’d)

baru

4alloc(baru)baru↑.info 4

baru↑.next bantu

baru↑.prev bantu↑.prev

Awal

5 97

Akhir

10

bantu

Middle Insertion (cont’d)

bantu↑.prev↑.next baru

Awal

5 97

Akhir

10

bantu

baru 4

bantu↑.prev baru

The last result for middle insertion if linked list wasn’t empty:

Middle Insertion (cont’d)

Awal

5 97

Akhir

104

baru

4

Awal

5 97

Akhir

10

bantu

DELETION

• Delete one node in beggining of linked list if linked list has only one node (awal = akhir).

Front Deletion

phapus awal

AwalAkhir

2

AwalAkhir

2

Awal

Akhir

elemen phapus↑.infoawal nilakhir nil

dealloc(phapus)

menjadi

If deletion happens in linked list with one node then linked list will be empty.

Front Deletion (cont’d)

2 4 5 9

Awal Akhir

phapus awalphapus

elemen phapus↑.info

elemen

Front Deletion (cont’d)

2 4 5 9

Akhir

phapus

awal awal↑.next atau awal phapus↑.next

Awal

Front Deletion (cont’d)

awal↑.prev nil dealloc(phapus)

2 4 5 9

Awal Akhir

phapus

The last result for front deletion if linked list has more than one node:

Front Deletion (cont’d)

4 5 9

Awal Akhir

4 5 9

Akhir

phapus

2

Awal

• 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

• If linked list has more than one node (awal ≠ akhir). For example, linked list has four nodes.

Back Deletion (cont’d)

2 4 5 9

Awal Akhir

phapus akhir

phapus

elemen phapus↑.info

elemen

Back Deletion (cont’d)

2 4 5 9

phapus

akhir akhir↑.prev atau akhir phapus↑.prev

Awal Akhir

Back Deletion (cont’d)

akhir↑.next nil

dealloc(phapus)

2 4 5 9

Awal Akhir phapus

The last result for back deletion if linked list has more than one node:

Back Deletion (cont’d)

2 4 5 9

Awal Akhir

phapus

• Middle deletion in double linked list is similar as middle deletion in single linked list.

Middle Deletion

Contact Person:Adam Mukharil Bachtiar

Informatics Engineering UNIKOMJalan Dipati Ukur Nomor. 112-114 Bandung 40132

Email: adfbipotter@gmail.comBlog: http://adfbipotter.wordpress.com

Copyright © Adam Mukharil Bachtiar 2012

GRACIASTHANK YOU

top related