04 - Single Linked List
04 - Single Linked List
10
1 4 7 12 15 20
1 4 7 10 12 15 20
2 7/26/2018
Insert into a sorted Array
Algorithm
while ( i < n ) and ( tab[i] < x ) do
i++
temp1 tab[i]
tab[i] x
j traversal [i+1..n]
temp2 tab[j]
tab[j] temp1
temp1 temp2
3 7/26/2018
Troublesome isn’t it?
that’s why we learn about Linked List
Dynamic Array
4 7/26/2018
Linked List
a data structure in which each element is
allocated dynamically and are bound with other
elements to form a linear relationship
This structure allows for efficient insertion or
removal of elements from any position in the
sequence
5 7/26/2018
Structure
Consists of nodes/elements
info next
6 7/26/2018
Element List
7 7/26/2018
Infotype
The data that we want to store
Define your own infotype
– Basic type example ElmList
Type infotype : integer
Type infotype : char
9 7/26/2018
ADT Element List
10 7/26/2018
Single Linked List
Type List : < First : address >
Dictionary
L
L : List
11 7/26/2018
Create New List
Algorithm /
12 7/26/2018
Creating New Element
Algorithm
Allocate(P) P
13 7/26/2018
Creating New Element
Algorithm
Allocate(P)
Next(P) Null
/
Info next
14 7/26/2018
Creating New Element
Algorithm
Allocate(P)
Next(P) Null
10
Info(P) 10 Info next
P
Next(Y)
– Select the next element of element Y
Info(Y)
– Select the data stored in element Y
16 7/26/2018
Exercise L
Task Answer
Output( Info( P ) )
Output( Info( first( L ) ) )
Output( Info( next( P ) )
P next( first( L ) )
Output( Info( next( P ) ) )
17 7/26/2018
Exercise L
Task Answer
Make P points the first element
Make P points the second element
Make P points the last element
Output info the first element of the list
Output info of the last element
18 7/26/2018
Exercise L
Task Answer
Copy info element P into first element
Copy info the second element into P
Set info of first element = 10
19 7/26/2018
Careful with Pointer
Suppose we have allocated two elements using
pointer P and Q
Then we change the pointer P so that P points to
the element pointed by Q
–P Q
15 / 30 /
20 7/26/2018
Careful with Pointer
The element (15) is lost
– Not deleted, but lost, and cannot be accessed
– Memory still used (wasted memory / leaked memory)
P Q
15 / 30 /
21 7/26/2018
Careful with Pointer
Suppose we have
L
15 30 40 /
22 7/26/2018
Question?
CSG2A3
ALGORITMA dan STRUKTUR DATA
Insert last
– New element became the last element of the list
25 7/26/2018
Insert First
Insert element P into List L so that P become the
first element of L
26 7/26/2018
Insert First
Algorithm
next(P) first(L)
first(L) P
P
15 /
L
30 40 50 /
27 7/26/2018
Insert Last
Insert element P into List L so that P become the
last element of L
28 7/26/2018
Insert Last
Dictionary
Q : address
Algorithm
Q first(L)
/* create a mechanism so that Q
points the last element */
P
next(Q) P
Q Q 60 /
L
30 40 50 /
29 7/26/2018
Insert After
Insert element P into List L so that P become the
next element of Prec
30 7/26/2018
Insert After
Algorithm
next(P) next(Prec)
next(Prec) P
Prec 45 /
L
30 40 50 /
31 7/26/2018
Deleting the Element
Delete first
– Remove the first element of the list
Delete last
– Remove the last element of the list
Delete after
– Remove an element next to a particular element
32 7/26/2018
Delete First
Remove the first element of L
33 7/26/2018
Delete First
Algorithm
P first(L)
first(L) next(P)
P next(P) Nil
15 / 30 40 50 /
34 7/26/2018
Delete Last
Remove the last element of L
35 7/26/2018
Delete Last
Dictionary
Q : address
Algorithm
Q first(L)
/* create a mechanism so that Q points
the element before the last element */
P next(Q)
next(Q) Nil
Q Q P
L
30 40 50 / 60 /
36 7/26/2018
Delete After
Remove element after the element pointed by
Prec
37 7/26/2018
Delete After
Algorithm
P next (Prec)
next(Prec) next(P)
next(P) Nil
P
Prec
L
30 40 45 / 50 /
38 7/26/2018
Mind the special conditions
Empty list
Only 1 element in list
39 7/26/2018
CAREFUL
NOTE that these delete functions only REMOVE
the element from the list and not completely
delete it
The “P” element are still in the memory
The “P” element still need to be DEALOCATED to
be fully deleted
40 7/26/2018
Question?
CSG2A3
ALGORITMA dan STRUKTUR DATA
Sequential search
what happens each time through the loop:
1) compare array[i] against key
2) increment counter i and compared against n
43 7/26/2018
Sequential Search with Sentinel
Sequential search
what happens each time through the loop:
1) compare array[i] against key
2) increment counter i and compared against n
44 7/26/2018
Sequential Search with Sentinel
Sequential search
what happens each time through the loop:
1) compare array[i] against key
2) increment counter i and compared against n
45 7/26/2018
Sequential Search with Sentinel
We can ensure that the item we're looking for is in
the array.
– By putting a copy of it at the end of the array
– This copy is called a sentinel
– Eliminate process of comparing counter i against n
46 7/26/2018
Sequential Search with Sentinel
47 7/26/2018
Exercise
Assumed that the insert functions (first, after,
last) are already defined, write a procedure
algorithm for sequential search using sentinel for
single linked list
48 7/26/2018
Insertion Sort
49
Insertion Sort
An Efficient sorting algorithm for (quite) small
data sets
– О(n2) comparisons
50 7/26/2018
Insertion Sort
Insertion sort on array
what happens each time through the loop:
1) Go through the elements in the unsorted part sequentially
2) Insert each element into the sorted part by searching for
it’s correct position
51 7/26/2018
Insertion Sort
Insertion sort on array
what happens each time through the loop:
1) Go through the elements in the unsorted part sequentially
2) Insert each element into the sorted part by searching for
it’s correct position