Sie sind auf Seite 1von 30

CSG2A3

ALGORITMA dan STRUKTUR DATA

Variations of Linked List

Double Linked List


Double Linked List
Linked list with 2 pointers
Connect previous and next element

First Last

/ 12 35 78 /

2 7/26/2018
Structure
Usually with 2 head pointer
First Last

/ /

Each element divided into 3 parts

Prev Info Next

3 7/26/2018
ADT Element Double Linked List
Type infotype : integer Prev Info Next
Type address : pointer to ElmList

Type ElmList <


info : infotype
next : address
prev : address
>

There are 2 pointer to point the next and previous


element
4 7/26/2018
ADT Double Linked List
Type List : <
First : address
Last : address L
>

Dictionary
L : List

There are 2 head to point the first and the last


element on the list
5 7/26/2018
Reuse the ADT
Try to feel the benefit of ADT
If you have already define ADT for single Linked
List, you just have to modify it a little to have an
ADT for double Linked List
Any modification only in specifying function
(implementation)

6 7/26/2018
Remember DRY Principles
“When you find yourself writing code that is
similar or equal to something you've written
before,
“take a moment to think about what you're doing
and don't repeat yourself.”

7 7/26/2018
Create New List
Algorithm
First(L)  Nil First last
L
Last(L)  Nil

Last(X) is a keyword to refer the last element of


the list X
On the creation of new list, there is no element,
thus first(L) and last(L) is Nil / Null

8 7/26/2018
Creating New Element
Algorithm
Allocate(P)
Next(P)  Nil
Prev(P)  Nil Prev Info next

Info(P)  10

Prev(Y) is a keyword to refer the previous


element of element pointed by Y
On the creation of new element, set Next and
Prev element = Nil
9 7/26/2018
Keywords
First(X)
Next(Y)
Info(Y)
Last(X)
Select the last element of list X

Prev(X)
– Select the previous element of element Y

10 7/26/2018
Exercise
P
L First Last

/ 12 43 68 80 89 /

Task : Draw the Pointer


Q  prev(prev(P))
R  next(prev(prev(last(L))))
S  next(next(prev(next(P))))

11 7/26/2018
Exercise
P
L First Last

/ 12 43 68 80 89 /

Task : How to … output Answer


Access info of the last element 89
Access info of the second element of the list 43
Access info of the fourth element of the list 80
Copy info of 4th element to 2nd element
Make P points 4th element

12 7/26/2018
Exercise
P
L First Last

/ 12 43 68 80 89 /

Task : what is the output? Answer


Info(Prev(next(prev(last(L))))
Info(Next(next(P)))
Info(prev(Prev(next(P))))
Info(Next(Next(Prev(Next(first(L)))))

13 7/26/2018
Exercise
P
L First Last

/ 12 43 68 80 89 /

Task : what is the output? Answer


Info(next(P)) + info(prev(last(L)))
Info(first(L)) – info(next(next(P)))
Info(prev(P)) – info(next(next(first(L))))
Info(Next(Next(Prev(Next(first(L))))) + info(P)

14 7/26/2018
Question?
Inserting new Element
Insert first
Insert last
Insert after

16 7/26/2018
Insert First
Algorithm
next(P)  first(L)
P prev(first(L))  P
first(L)  P
/ 6 /
L First
Last

/ 12 68 89 /

WHAT WILL HAPPEN IF THE


LIST IS EMPTY?

17 7/26/2018
Insert First on empty list
Algorithm

P next(P)  first(L)
prev(first(L))  P ERROR

/ 43 / first(L)  P
L First Last

/ / // if list is empty
first(L)  P
last(L)  P

18 7/26/2018
Insert First
Algorithm
If (first(L) ≠ Nil and last(L) ≠ Nil ) then
next(P)  first(L)
prev(first(L))  P
first(L)  P
else
first(L)  P
last(L)  P

19 7/26/2018
Insert Last
Algorithm
prev(P) = Last(L)
next(last(L)) = P P
last(L) = P
/ 99 /

L First Last

/ 12 68 89 /

Again, careful when the list is empty

20 7/26/2018
Insert After
Algorithm
next(P)  next(Prec)
prev(P)  Prec
prev(next(prec))  P
next(Prec)  P
P
Prec
/ 77 /
L First Last

/ 12 68 89 /

21 7/26/2018
Deleting the Element
Delete first
Delete last
Delete after

22 7/26/2018
Delete First
Algorithm
P  first(L)
first(L)  next(P)
WHAT WILL HAPPEN IF THERE
next(P)  Nil
IS ONLY 1 ELEMENT INSIDE
THE LIST ? prev(first(L))  Nil

P
P

L First Last

/ 12 / / 43 80 89 /

23 7/26/2018
Delete First
Algorithm
P  first(L)
first(L)  next(P)
next(P)  Nil
ERROR prev(first(L))  Nil

P
//if only one element
first(L)  Nil
L First Last
last(L)  Nil
/ 12 /
P

24 7/26/2018
Delete First
Algorithm
P  first(L)
If (first(L) ≠ last(L)) then
first(L)  next(P)
next(P)  Nil
prev(first(L))  Nil
else
first(L)  Nil
last(L)  Nil

P

25 7/26/2018
Delete Last
Algorithm
P  Last(L)
Last(L)  prev(last(L))
prev(P)  Nil
next(last(L))  Nil

P P

L First Last

/ 12 43 / / 89 /

26 7/26/2018
Delete After
Algorithm
P  next(Prec)
Next(Prec)  next(P)
Prev(next(P))  Prec
Prev(P)  Nil
Next(P)  Nil

P Prec P

L First Last

/ 12 43 / 80 / 89 /

27 7/26/2018
Question?
Home Task
Modify your previous task (single linked list) into
double linked list
Write each procedure of insert and delete
Write a function/procedure to search an element
by id and output the info of the element

Note : job description should be different from the


previous task

29 7/26/2018
THANK YOU
7/26/2018
30

Das könnte Ihnen auch gefallen