Download Foundation of Computing Systems

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Foundation of Computing Systems
Lecture 2
Linked Lists
Array vs. Linked List
• Array
– elements are stored in a contagious memory
locations
– static data structure
• Linked list
– adjacency between any two elements are
maintained by means of links or pointers
– dynamic data structures
29.07.09
IT 60101: Lecture #2
2
Linked List
• A linked list is an ordered collection of finite,
homogeneous data elements called nodes
where the linear order is maintained by means
of links or pointers
LINK
DATA
.
Link to the next node
• Single linked list, circular linked list, and double
linked list
29.07.09
IT 60101: Lecture #2
3
Linked List Representation: Static
HEADER
Header
59
38
64
N1
N2
N3
80
N6
14
72
N4
N5
DATA
LINK
41
42
43
44
45
46
47
48
49
38
14
59
80
72
50
45
50
.
.
.
64
.
.
.
43
.
.
.
Memory
Location
29.07.09
IT 60101: Lecture #2
47
41
Array of
pointers
4
Linked List Representation: Dynamic
AVAIL
NEW
X
XY
HEADER
X
XY
29.07.09
IT 60101: Lecture #2
5
Linked List Representation: Dynamic
AVAIL
HEADER
X
29.07.09
IT 60101: Lecture #2
6
Operations on Single Linked List
• Traversing a list
– Searching for an element in a list
• Insertion of a node into a list
• Deletion of a node from a list
• Copy a linked list to make a duplicate
• Merging two linked lists into a larger list
29.07.09
IT 60101: Lecture #2
7
Single Linked List: Insertion
• Insertion steps
– Get a new node from memory bank
– Start from the header node
– Manage links to
• Insert at front
• Insert at end
• Insert at any position
29.07.09
IT 60101: Lecture #2
8
Single Linked List: Insert at Front
new
From
memory
bank
HEADER
2
X
29.07.09
X
1
IT 60101: Lecture #2
9
Single Linked List: Insert at End
new
HEADER
X
1
ptr
29.07.09
IT 60101: Lecture #2
10
Single Linked List: Insert at Any Place
new
HEADER
X
ptr
2
KEY
29.07.09
1
X
IT 60101: Lecture #2
11
Single Linked List: Deletion
• Deletion steps
– Start from the header node
– Manage links to
• Delete at front
• Delete at end
• Delete at any position
– Return the deleted node to memory bank
29.07.09
IT 60101: Lecture #2
12
Single Linked List: Delete at Front
HEADER
X
1
ptr1
ptr
KEY
29.07.09
X
IT 60101: Lecture #2
13
Single Linked List: Delete at End
HEADER
Return to the memory bank
ptr1
ptr
X
29.07.09
IT 60101: Lecture #2
14
Single Linked List: Delete at Any Place
HEADER
ptr1
ptr
1
X
KEY
Return to the memory bank
29.07.09
IT 60101: Lecture #2
15
Single Linked List: Copy
HEADER1
L1
...
HEADER2
To memory bank
L2
X
...
29.07.09
IT 60101: Lecture #2
16
Circular Linked List
HEADER
29.07.09
IT 60101: Lecture #2
17
Merging Two Circular Linked Lists
...
X
HEADER1
1
ptr1
ptr2
ptr2
2
HEADER2
...
X
Return to memory
29.07.09
IT 60101: Lecture #2
18
Double Linked List
LLINK
RLINK
DATA
...
29.07.09
IT 60101: Lecture #2
19
Double Linked List: Insertion
NEW
HEADER
 
 
ptr
...
a) Insertion at the front
ptr
HEADER
...


b) Insertion at the end
NEW
ptr

29.07.09
 

NEW
IT 60101: Lecture #2
c) Insertion at any intermediate position
20
Double Linked List: Deletion
HEADER
ptr

ptr1
X
...
X

Return to the memory bank
a) Deletion at the front
HEADER
ptr
ptr1

X
...
b) Deletion at the end
HEADER
ptr
ptr1
ptr2

X
KEY
X

Return to the memory bank
29.07.09
IT 60101: Lecture #2
c) Deletion at an intermediate position
21
Applications of Linked Lists
• Sparse matrix manipulation
• Polynomial manipulation
• Memory management
29.07.09
IT 60101: Lecture #2
22
Application of Linked List: Sparse Matrix
j
i
ROWLINK
DATA
COLLINK
29.07.09
IT 60101: Lecture #2
23
Application of Linked List: Sparse Matrix
Column
Row
29.07.09
1
2
3
4
5
1
*
*
A
*
*
2
*
X
*
*
M
3
*
*
*
*
*
4
P
*
*
*
O
5
*
*
L
*
*
6
K
C
*
*
B
IT 60101: Lecture #2
24
Application of Linked List: Sparse Matrix
HEADER
6
RH1
RH2
RH3
1
5
RH6
29.07.09
0
CH2
2
CH3
1
0
CH4
CH5
3
A
2
0
2
2
2
3
0
4
0
4
4
1
6
5
0
0
6
1
K
Points to HEADER
5
O
P
5
5
M
X
RH4
RH5
CH1
1
0
3
L
2
6
C
IT 60101: Lecture #2
6
5
B
25
Application of Linked List: Polynomial
P(x) = anxen + an–1xen–1 + · · · + a1xe1
C O E F F E XP O
3
8
-7
6
14
LIN K
3
10
1
-5
0
P(x) = 3x8 – 7x6 + 14x3 + 10x – 5
29.07.09
IT 60101: Lecture #2
26
Application of Linked List: Polynomial
• For polynomial manipulation
See the book
Classic Data Structures
Chapter 3
PHI, 2nd Edn., 17th Reprint
29.07.09
IT 60101: Lecture #2
27
Application of Linked List: Memory
• For memory management
See the book
Classic Data Structures
Chapter 3
PHI, 2nd Edn., 17th Reprint
29.07.09
IT 60101: Lecture #2
28
Related documents