0% found this document useful (0 votes)
34 views

04 - Single Linked List

The document discusses various algorithms for single linked lists, including insertion, deletion, and searching. It introduces the concept of a sentinel node to improve the efficiency of sequential search on a linked list. Specifically, using a sentinel node allows the search algorithm to eliminate one of the comparison processes typically inside the search loop, making the loop faster. The document provides pseudocode examples for inserting elements at different positions in the list, deleting elements, and searching with a sentinel. It emphasizes the need to properly handle special cases like empty lists and properly de-allocating memory when deleting elements from the list.

Uploaded by

rakhaadit
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

04 - Single Linked List

The document discusses various algorithms for single linked lists, including insertion, deletion, and searching. It introduces the concept of a sentinel node to improve the efficiency of sequential search on a linked list. Specifically, using a sentinel node allows the search algorithm to eliminate one of the comparison processes typically inside the search loop, making the loop faster. The document provides pseudocode examples for inserting elements at different positions in the list, deleting elements, and searching with a sentinel. It emphasizes the need to properly handle special cases like empty lists and properly de-allocating memory when deleting elements from the list.

Uploaded by

rakhaadit
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

CSG2A3

ALGORITMA dan STRUKTUR DATA

Single Linked List


Introduction
Exercise
Create an algorithm to insert a number into an
ordered array of integer so that the array result
remain ordered

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

Generally, each Element is divided into 2 parts

info next

6 7/26/2018
Element List

Type ElmList < ElmList


info : infotype What is infotype ?
next : address
> What is address ?

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

– Record type example


Type infotype :
mahasiswa <
nim : string
name : string
>
8 7/26/2018
Address
Pointer to element
Type address : pointer to ElmList

9 7/26/2018
ADT Element List

Type infotype : integer


Type address : pointer to ElmList
ElmList
Type ElmList <
info : infotype
next : address
>

10 7/26/2018
Single Linked List
Type List : < First : address >

Dictionary
L
L : List

Only create the list variable

11 7/26/2018
Create New List
Algorithm /

First(L)  Nil first

First(X) is a keyword to know the first element of


the list X
– Use First(X) instead of X.First

On the creation of new list, there is no element,


thus first(L) is Nil / Null

12 7/26/2018
Creating New Element
Algorithm
Allocate(P) P

Allocating space memory for an element


– According to the size defined by the element type

Only the pointer that knows where the element


resides

13 7/26/2018
Creating New Element
Algorithm
Allocate(P)
Next(P)  Null
/
Info next

Next(Y) is a keyword to know the next element


of element pointed by Y
– Likewise, use Next(Y) instead Y.Next

On the creation of new element,


set Next element = Null

14 7/26/2018
Creating New Element
Algorithm
Allocate(P)
Next(P)  Null
10
Info(P)  10 Info next

P

Info(Y) is a keyword to access the data stored in


the element
– If infotype is a record type, operation is like a normal
record operation
– Info(P).nim  ‘11031300xx’
15 7/26/2018
Keywords
First(X)
– Select the first element of list X

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

Then what will happen to element (15) ?


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 /

What will happen if we run the operation


– First(L)  next(first(L))

Element 15 cannot be accessed


– Again, lost, and cannot be accessed
– Memory still used (wasted memory)

22 7/26/2018
Question?
CSG2A3
ALGORITMA dan STRUKTUR DATA

Single Linked List


Insertion and Deletion
Inserting new Element
Insert first
– New element became the first element of the list

Insert last
– New element became the last element of the list

Insert after / Insert before


– Put the element somewhere in the middle

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

Searching and Sorting on

Single Linked List


Sequential Search with Sentinel
Supposedly, sequential search using sentinel is a
searching algorithm aimed to reduce the
complexity which is more suitable for linked list
– Rather than using it on array

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

There are 2 comparison processes inside the loop


The aim is to eliminate one of those comparisons
– So the loop might be a lot faster

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

We need step (2) because:


– We might run off the end of array[] which might causing
undefined behavior,
– Obviously because we aren't sure that key is in array[].

If we knew that the key was in array[], then we


could skip step (2)

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

The application is better using pointer


– Eliminate process of increment counter i

46 7/26/2018
Sequential Search with Sentinel

Sequential search using sentinel


what happens each time through the loop:
1) compare info(P) against key
2) next( P )

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

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

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

Step (2) on array costs O(n) since there is the


possibility of swapping process for each insertion
Therefore the cost of the entire algorithm is O(n2)
Nb: you’ll learn more about this next year

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

Using linked list, there is no swapping process in


insertion
– Cost of step (2) is O(1)

Therefore the cost of the entire algorithm became


O(n)
52 7/26/2018
Question?
THANK YOU
7/26/2018
54

You might also like