Sie sind auf Seite 1von 25

CSG2A3

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

2 7/26/2018
Circular Linked List

L
15 30 40

Last
First

12 35 78

3 7/26/2018
Circular Linked List
L
30

15 40

Last
First
35

12 78

4 7/26/2018
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

5 7/26/2018
Exercise

Task : Draw the Pointer


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

6 7/26/2018
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)))))

7 7/26/2018
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)

8 7/26/2018
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

10 7/26/2018
Insert to Empty List
single list circular

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

15 /

11 7/26/2018
Delete the last element
single list circular

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

12 7/26/2018
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

13 7/26/2018
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

14 7/26/2018
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

15 7/26/2018
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 /

16 7/26/2018
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 /

17 7/26/2018
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 /

18 7/26/2018
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

19 7/26/2018
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

20 7/26/2018
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 /

21 7/26/2018
Insert After and Delete After
Same as normal single linked list and double
linked list

22 7/26/2018
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

24 7/26/2018
THANK YOU
7/26/2018
25

Das könnte Ihnen auch gefallen