06 - Linked List Variation - Circular Linked List
06 - Linked List Variation - Circular Linked 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
6 7/26/2018
Exercise
7 7/26/2018
Exercise
8 7/26/2018
Question?
Same Structure
Same element and list
– ADT
– Create new list
– Allocation / create new element
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
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
24 7/26/2018
THANK YOU
7/26/2018
25