Вы находитесь на странице: 1из 28

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

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
DA TA

Link to the next node

Single linked list, circular linked list, and double


linked list
29.07.09

IT 60101: Lecture #2

Linked List Representation: Static

H EAD ER

H eader
59

38

64

N 1

N 2

N 3
80
N 6

14

72

N 4

N5

D ATA

L IN K

41
42
43
44
45
46
47
48
49

38
14
59
80
72

50

45

50
.
.
.

64
.
.
.

43
.
.
.

M e m o ry
L o c a tio n

29.07.09

IT 60101: Lecture #2

47
41

A rra y o f
p o in te r s

Linked List Representation: Dynamic


A V A IL

N E W

X Y
H E A D E R

X Y

29.07.09

IT 60101: Lecture #2

Linked List Representation: Dynamic


A V A IL

HEADER

29.07.09

IT 60101: Lecture #2

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

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

Single Linked List: Insert at Front


new

F ro m
m e m o ry
bank

H EAD ER
2
X

29.07.09

X
1

IT 60101: Lecture #2

Single Linked List: Insert at End


new

H EAD ER

X
1
p tr

29.07.09

IT 60101: Lecture #2

10

Single Linked List: Insert at Any Place


new

H EAD ER
X
p tr

KEY

29.07.09

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


H EAD ER

p tr1

p tr

KEY

29.07.09

IT 60101: Lecture #2

13

Single Linked List: Delete at End


HEADER
R e tu rn to th e m e m o ry b a n k

p tr1

p tr

29.07.09

IT 60101: Lecture #2

14

Single Linked List: Delete at Any Place


H EAD ER

p tr1

p tr
1
X

KEY

R e tu rn to th e m e m o ry b a n k

29.07.09

IT 60101: Lecture #2

15

Single Linked List: Copy


H EAD ER 1

L1

. . .
H EAD ER 2

T o m e m o ry b a n k

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

. . .

HEADER1
1

p tr1

p tr2

p tr2
2
HEADER2

. . .

R e tu rn to m e m o ry

29.07.09

IT 60101: Lecture #2

18

Double Linked List


L L IN K

R L IN K
D ATA

. . .

29.07.09

IT 60101: Lecture #2

19

Double Linked List: Insertion


NEW

HEADER

p tr

. . .
a ) I n s e r t io n a t t h e f r o n t
p tr

HEADER

. . .

b ) In s e r tio n a t th e e n d
NEW

p tr

29.07.09

NEW

IT 60101: Lecture #2

c ) I n s e r t io n a t a n y in t e r m e d ia t e p o s it io n

20

Double Linked List: Deletion


HEAD ER

p tr

p tr1

. . .

R e tu rn to th e m e m o ry b a n k
a ) D e le t io n a t t h e f r o n t

HEADER

p tr

p tr1

. . .

b ) D e le t io n a t t h e e n d

HEAD ER

p tr

p tr1

p tr2

KEY

R e tu rn to th e m e m o ry b a n k

29.07.09

IT
c ) D 60101:
e l e t i o n a t aLecture
n i n t e r m e d i #2
a t e p o s it io n

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

R O W L IN K

DATA

C O L L IN K

29.07.09

IT 60101: Lecture #2

23

Application of Linked List: Sparse Matrix


C o lu m n
1

R ow

29.07.09

IT 60101: Lecture #2

24

Application of Linked List: Sparse Matrix


HEADER

RH1

RH2

RH3

R H4

RH5

RH6

CH1
1

C H2
2

CH3

CH4

CH5

3
A

5
M

1
P

3
L

1
K

P o in ts t o H E A D E R

29.07.09

5
O

6
C

IT 60101: Lecture #2

5
B

25

Application of Linked List: Polynomial


P(x) = anxen + an1xen1 + + a1xe1

COEFF EXPO

-7

14

LINK

10

-5

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

Вам также может понравиться