Professional Documents
Culture Documents
05 - Linked List Variation - Double Linked List
05 - Linked List Variation - Double Linked List
05 - Linked List Variation - Double Linked List
First Last
/ 12 35 78 /
2 7/26/2018
Structure
Usually with 2 head pointer
First Last
/ /
3 7/26/2018
ADT Element Double Linked List
Type infotype : integer Prev Info Next
Type address : pointer to ElmList
Dictionary
L : List
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
8 7/26/2018
Creating New Element
Algorithm
Allocate(P)
Next(P) Nil
Prev(P) Nil Prev Info next
Info(P) 10
Prev(X)
– Select the previous element of element Y
10 7/26/2018
Exercise
P
L First Last
/ 12 43 68 80 89 /
11 7/26/2018
Exercise
P
L First Last
/ 12 43 68 80 89 /
12 7/26/2018
Exercise
P
L First Last
/ 12 43 68 80 89 /
13 7/26/2018
Exercise
P
L First Last
/ 12 43 68 80 89 /
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 /
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 /
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
29 7/26/2018
THANK YOU
7/26/2018
30