You are on page 1of 95

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 04/16/2020
Structure
Usually with 2 head pointer
First Last

/ /

Each element divided into 3 parts


Prev Info Next

3 04/16/2020
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 04/16/2020
ADT Double Linked List
Type List : <
FIRST LAST
First : address
Last : address L

>

Dictionary
L : List

There are 2 head to point the first and the last


element on the list
5 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
Creating New Element
Algorithm P
Allocate(P)
Next(P)  Nil
/ 10 /
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
Question?
Inserting new Element
Insert first
Insert last
Insert after

16 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
Deleting the Element
Delete first
Delete last
Delete after

22 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
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 04/16/2020
THANK YOU
04/16/2020
30
ALGORITMA dan STRUKTUR DATA

Variations of Linked List

Circular Linked List


Circular Linked List
Linked list where the last element of the list is
connected to the first element
It supports traversing from the end of the list to
the beginning by making the last node point back
to the head of the list

32 04/16/2020
Circular Linked List

L
15 30 40

Last
First

12 35 78

33 04/16/2020
Circular Linked List
L
30

15 40

Last
First
35

12 78

34 04/16/2020
Circular Linked List
Circular linked lists are usually sorted
Circular linked lists are useful for playing video
and sound files in “looping” mode
They are also a stepping stone to implementing
graphs

35 04/16/2020
Exercise

Task : Draw the Pointer


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

36 04/16/2020
Exercise

Task : what is the output? Answer


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

37 04/16/2020
Exercise

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(last(L))))
Info(Next(Next(Prev(Next(last(L))))) + info(P)

38 04/16/2020
Question?
Same Structure
Same element and list
– ADT
– Create new list
– Allocation / create new element

Different in inserting and deleting


– Insert first and last
– Delete first and last

40 04/16/2020
Insert to Empty List
single list circular

Algorithm
next(P)  first(L)
first(L)  P
next(P)  first(L)
P
L

15 /

41 04/16/2020
Delete the last element
single list circular

Algorithm
P  first(L)
next(P)  Nil
first(L)  Nil
P
L P
15 /

42 04/16/2020
Insert Last
single list circular
Dictionary
Q : address
Algorithm
/* make a mechanism so that Q
points the last element */
next(P)  first(L) P
next(Q)  P
Q 15 /
L

30 50

43 04/16/2020
Insert First
single list circular
Dictionary
Q : address
Algorithm
/* make a mechanism so that Q
L
points the last element */
next(P)  first(L) P
next(Q)  P
first(L)  P Q 15
L

30 50

44 04/16/2020
Delete First
single list circular
Dictionary
Q : address
Algorithm
/* make a mechanism so that Q
points the last element */
P  first(L)
first(L)  next(P)
P L
next(P)  Nil Q
L
next(Q)  first(L)

30 / 50 15
P

45 04/16/2020
Delete Last
single list circular
Dictionary
Q : address
Algorithm
/* make a mechanism so that Q
points the element before the last element */
P  next(Q)
next(Q)  first(L)
next(P)  Nil Q P
L

P
30 50 15 /

46 04/16/2020
Insert to Empty List
double list circular

Algorithm
first(L)  P
next(P)  first(L)
prev(P)  first(L)
last(L)  P
P
Last
First

/ 12 /

47 04/16/2020
Delete the last element
double list circular

Algorithm
P  first(L)
next(P)  Nil
prev(P)  Nil
first(L)  Nil
P
last(L)  Nil
Last
First
P
/ 12 /

48 04/16/2020
Insert First and Insert Last
double list circular
Algorithm
next(P)  first(L)
prev(P)  last(L)
next(last(L))  P
prev(first(L))  P
P
last
// for insert first
/ 78 /
first(L)  P
First

// for insert last 12 35


last(L)  P

49 04/16/2020
Delete First
double list circular
Algorithm
P  first(L)
first(L)  next(P)
next(P)  Nil
prev(P)  Nil
prev(first(L))  last(L)
First
next(last(L))  first(L)
P
Last
First
P

/ 12 / 35 78

50 04/16/2020
Delete Last
double list circular
Algorithm
P  Last(L)
Last(L)  prev(last(L))
prev(P)  Nil
next(P)  Nil
prev(first(L))  last(L) Last

next(last(L))  first(L)
P Last
First
P

12 35 / 78 /

51 04/16/2020
Insert After and Delete After
Same as normal single linked list and double
linked list

52 04/16/2020
Question?
Home Task
Modify your previous task into circular linked list
(double or single)
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 tasks

54 04/16/2020
THANK YOU
04/16/2020
55
ALGORITMA dan STRUKTUR DATA

Variations of Linked List

Multi Linked List


Multi Linked List
Linked list where each node may contain pointers
to more than one nodes
– Double linked list is also considered multi linked list

Represents Parent-child relation


– Tree
– Graph

57 04/16/2020
Multi Linked List – parent child relation

Modification of single or double linked list so that


they can perform better with the case at hand
Aimed to illustrate the relation between data
– 1 to N relation  tree
– N to M relation  graph

In form of :
– List inside a list
– Connection between 2 or more list

58 04/16/2020
Example : Student - Class
Let’s say we want to store student data and its
relation with the default class
What is the relation of the data?
– 1 to N relation

59 04/16/2020
Example : Student - Class
Type infotype_student < Type infotype_class <
id : string class_name : string
name : string supervisor : string
> >

Type adr_student : pointer to Type adr_class : pointer to


elm_student elm_class

Type elm_student < Type elm_class <


info : infotype_student info : infotype_class
next : adr_student next : adr_class
> >

60 04/16/2020
Example : Student - Class
Type list_student < Type list_class <
first : adr_student first : adr_class
> >

L1 : list_student
L2 : list_class

61 04/16/2020
Example : Student - Class

Now, how to draw


the relation between data?

62 04/16/2020
Example : Student - Class
1-to-n relation

L1

A / B / C / D / /

L2
The simplest way is
X Y Z / to add a pointer to elm_class
in elm_student to show which
Class the student is enrolled

63 04/16/2020
Example : Student - Class
Type infotype_student < Type infotype_class <
id : string class_code : string
name : string class_name : string
> credit : integer
>
Type adr_student : pointer to
elm_student Type adr_class : pointer to
elm_class
Type elm_student <
info : infotype_student Type elm_class <
next : adr_student info : infotype_class
nextClass : adr_class next : adr_class
> >

64 04/16/2020
Operations
Insert Student and Insert Course
– No change

Search Student and Search Class


– No change

Set Class for a Student


Search Class of a Student
Search Student(s) of a Class

65 04/16/2020
Operations
Delete Student
– No change

Delete Relation
Delete Class

66 04/16/2020
Procedure Set Class
Procedure set_class(i/o: L1:list_student, i: L2 : list_class,
id : string, class : string)
Dictionary
P : adr_student
Q : adr_class
Algorithm
P  search_student(L1, id)
Q  search_class(L2, class)
if ( P ≠ Nil and Q ≠ Nil ) then
nextClass(P)  Q

67 04/16/2020
Procedure Delete Class
Procedure del_class(i/o: L1:list_student, L2 : list_class, i: class : string)
Dictionary
P : adr_student
Q : adr_class
Algorithm
Q  search_class(L2, class)
if ( Q ≠ Nil ) then
P  first(L1)
while (P ≠ Nil) do
if ( nextClass(P) = Q ) then
nextClass(P) = Nil
P  next(P)
deleteClass (L2, Q)

68 04/16/2020
Example : Student - Course
Let’s say we want to store student data and its
relation with the course taken
What is the relation of the data?
– N to M relation

69 04/16/2020
Example : Student - Course
Type infotype_student < Type infotype_course <
id : string course_id : string
name : string course_name : string
> credit : integer
>

Type adr_student : pointer to Type adr_course : pointer to


elm_student elm_course

Type elm_student < Type elm_course <


info : infotype_student info : infotype_course
next : adr_student next : adr_course
> >

70 04/16/2020
Example : Student - Course
Type list_student < Type list_course <
first : adr_student first : adr_course
> >

L1 : list_student
L2 : list_course

71 04/16/2020
Example : Student - Course

Now, how to draw


the relation between data?

72 04/16/2020
Example : Student - Course
n-to-n relation
L1

A / B / C / D / /

X Y / X

Y / Z /

L2 For this case,


insert a list of course
inside each elm_student
X Y Z /
to indicate every course
taken by the student

73 04/16/2020
Example : Student - Course
Type infotype_student < Type infotype_course <
id : string course_id : string
name : string course_name : string
> credit : integer
>
Type adr_student : pointer to
elm_student Type adr_course : pointer to
elm_course
Type elm_student <
info : infotype_student Type elm_course <
next : adr_student info : infotype_course
course : list_course next : adr_course
> >

74 04/16/2020
Operations
Insert Student and Insert Course
– No change

Search Student and Search Class


– No change

Add course to a student


Delete course in a student
Delete a student
Delete a course
75 04/16/2020
Procedure add Course
Procedure add_course(i/o: L1:list_student,
i: L2 : list_course, id : string, course_id : string)

Dictionary
P : adr_student
Q, R : adr_course …
L3 : list_course if ( P ≠ Nil and Q ≠ Nil ) then
Algorithm Allocate(R)
P  search_student(L1, id) info(R)  info(Q) //duplicate Q
Q  search_course(L2, L3  course(P)
id_course) insertLastCourse(L3, R)

76 04/16/2020
Procedure Delete Course Student
Procedure del_course_student(i/o: L1:list_student,
i: id : string, course_id : string)
Dictionary
P : adr_student
L3 : list_course
Algorithm
P  search_student(L1, id)
if ( P ≠ Nil ) then
L3  course(P)
deleteCourse(L3, course_id)

77 04/16/2020
Procedure Delete Student
Procedure del_student(i/o: L1:list_student, i: id : string)
Dictionary
P : adr_student
L3 : list_course
Algorithm
P  search_student(L1, id)
if ( P ≠ Nil ) then
L3  course(P)
emptyList(L3)
deleteStudent(L1, info(P).id )

78 04/16/2020
Procedure Delete Course
Procedure del_course(i/o: L1:list_student,
i: L2:list_course, id_course : string)
Dictionary
P : adr_student
Q : adr_course
L3 : list_course
Algorithm
P  first(L1)
while ( P ≠ Nil ) do
L3  course(P)
deleteCourse(L3, course_id)
P  next(P)
deleteCourse(L2, course_id)

79 04/16/2020
Which one is the parent?

80 04/16/2020
Variation of Multi List
Depend on the case, List L2 might not be needed
– When child list is not too important to be listed
independently
– When the child list is too vary
– Example : List of Employee and list of their children

81 04/16/2020
Procedure add Course
Procedure add_course(i/o: L1:list_student, i: L2 : list_course,
id : string)
Dictionary
P : adr_student

R : adr_course
if ( P ≠ Nil ) then
L3 : list_course
Allocate(R)
Algorithm
input(info(R)) //input new data for
P  search_student(L1, id) child
… L3  course(P)
insertLastCourse(L3, R)

82 04/16/2020
Question?
Alternative Solution
With the duplication of the child node, the size
might expand became too big
Difficult modification at child node
Solution :
– Make the child list is a pointer element to point the second
list as a relation list

84 04/16/2020
Alternative : Student - Course
Type infotype_student < Type infotype_course <
id : string course_id : string
name : string course_name : string
> credit : integer
Type adr_student : pointer to >
elm_student Type adr_course : pointer to elm_course

Type elm_student < Type elm_course <


info : infotype_student info : infotype_course
next : adr_student next : adr_course
course : list_relation >
>
Type adr_relation : Type list_relation <
pointer to elm_relation first : adr_relation
>
Type elm_relation <
next_course : adr_course
next : adr_relation
>

85 04/16/2020
Example : Student - Course
n-to-n relation
L1

A B / C D /

/ /

L2

X Y Z /

86 04/16/2020
Procedure add Course
Procedure add_course(i/o: L1:list_student, i: L2 : list_course,
id : string, course_id : string)
Dictionary
P : adr_student …
Q : adr_course if ( P ≠ Nil and Q ≠ Nil ) then
R : adr_relation Allocate(R)
L3 : list_course next_course(R)  Q
Algorithm //create pointer to Q
P  search_student(L1, id) L3  course(P)
Q  search_course(L2, insertLastCourse(L3, R)
id_course)

87 04/16/2020
Question?
P

Exercise 1

1 Info( P )
Info( child( P ) )
2 Info( child( next( P ) ) )
3 Info( next( child( first( L1 ) ) ) )
4 Info( next( P ) )
P  next( P )
5 Child( P )  next( first( L2 ) )
Info( child( P ) )
6 create algorithm to count member of L1

89 04/16/2020
P

Exercise 2

1 Create algorithm to count the parent of Q


P  first( L1 )
Q  child( P )
2 info( Q )  info( next( Q )
Child( next( P ) )  next( Q )
Output( Info( child( P ) ) )
Output( Info( child( next( P ) ) ) )
3 Make child A = Y

90 04/16/2020
P

Exercise 3

1 info( first( L1 ) ) + info( child( P ) )


2 Q  next( first( L2 ) )
Info( Q ) + info( child( next(P) ) )
3 P  next( next( P ) )
Info( next( child( first( L1 ) ) ) ) – info( P )
4 Create algorithm to show info parent with
no child

91 04/16/2020
P
Q
Exercise 4

1 Info( next( child( first( L1 ) ) ) )


P  next( P )
Q  child( P )
Info( Q ) + info( P )
2
Q  next( child( next( P ) ) ) )
P  next( first( L1 ) )
Info( P ) - info( Q )
3 Create algorithm to count child of P

92 04/16/2020
Home Task
Modify your previous task into multilinked list with
m-n relation
– Create a child infotype that will suit the relation

Write each procedure of insert and delete


– Including add and remove relation

Note : job description should be different from the


previous tasks

93 04/16/2020
“special” Home Task
Let 2 linked lists that store computer data
(Computer name, IP Address) and user data
(username, password)
There is a restriction case where 1 user can only
login in 1 computer
– 1-to-1 relationship

Design a data structure that will ensure 1-to-1


relation between computer data and user data

94 04/16/2020
THANK YOU
04/16/2020
95

You might also like