+ All Categories
Home > Documents > Foundation of Computing Systems Lecture 2 Linked Lists.

Foundation of Computing Systems Lecture 2 Linked Lists.

Date post: 15-Dec-2015
Category:
Upload: alexandrea-pettijohn
View: 215 times
Download: 1 times
Share this document with a friend
28
Foundation of Computing Systems Lecture 2 Linked Lists
Transcript

Foundation of Computing Systems

Lecture 2

Linked Lists

29.07.09 IT 60101: Lecture #2 2

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 3

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

• Single linked list, circular linked list, and double linked list

DATA

LINK

. Link to the next node

29.07.09 IT 60101: Lecture #2 4

Linked List Representation: Static

HEADER

59

N1

38

N2

64

N3

14

N4

72

N5

80

N6

38-

145980-

72

64

47

43

50

41

45

D A TA LIN K

414243

444546474849

50

H eader

.

.

.

.

.

.

.

.

.

M em oryLocation

A rray o fpo in ters

29.07.09 IT 60101: Lecture #2 5

Linked List Representation: DynamicA V A IL

N E W

H E AD E R X

XXY

XY

29.07.09 IT 60101: Lecture #2 6

Linked List Representation: DynamicA V A IL

H E A D E R

X

29.07.09 IT 60101: Lecture #2 7

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 8

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 9

Single Linked List: Insert at Front

HEADER

X

new

1

2

X

Fromm em orybank

29.07.09 IT 60101: Lecture #2 10

Single Linked List: Insert at End

HEADER X

new

ptr

1

29.07.09 IT 60101: Lecture #2 11

Single Linked List: Insert at Any Place

HEADER

X

new

ptr

KEY

12

X

29.07.09 IT 60101: Lecture #2 12

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 13

Single Linked List: Delete at Front

H EAD ER

KEY

ptr p tr1

1X

X

29.07.09 IT 60101: Lecture #2 14

Single Linked List: Delete at End

HEADER

ptr1 ptr

X

Return to the m em ory bank

29.07.09 IT 60101: Lecture #2 15

Single Linked List: Delete at Any Place

H EAD ER

KEY

ptr1 ptr

X

1

Return to the memory bank

29.07.09 IT 60101: Lecture #2 16

Single Linked List: Copy

. . .

. . .

HEADER1

HEADER2

L1

L2X

To memory bank

29.07.09 IT 60101: Lecture #2 17

Circular Linked List

HEADER

29.07.09 IT 60101: Lecture #2 18

Merging Two Circular Linked Lists

. . .

. . .

1

2

X

X

HEADER1

HEADER2

ptr1

ptr2ptr2

R etu rn to m em ory

29.07.09 IT 60101: Lecture #2 19

Double Linked List

. . .

D ATA

LLIN K R LIN K

29.07.09 IT 60101: Lecture #2 20

Double Linked List: Insertion

. . .

N E W

H E A D E R ptr

a) Insertion a t the fron t

. . .

H E A D E R ptr

N E W

b) Insertion a t the end

ptr

N E W

c) Insertion a t any in te rm ed ia te position

29.07.09 IT 60101: Lecture #2 21

Double Linked List: Deletion

. . .

H EAD ER

ptr

a) D e le tion a t the fron t

. . .

H EAD ER ptr

b) D e le tion a t the end

c) D e le tion a t an in term edia te position

ptr

R eturn to the m em ory bank

1

H EAD ERptr p tr2

R eturn to the m em ory bank

X

X

ptr1

ptr1

X

X

XK E Y

29.07.09 IT 60101: Lecture #2 22

Applications of Linked Lists

• Sparse matrix manipulation

• Polynomial manipulation

• Memory management

29.07.09 IT 60101: Lecture #2 23

Application of Linked List: Sparse Matrix

i j

D ATA

R O W LIN K

C O LLIN K

29.07.09 IT 60101: Lecture #2 24

Application of Linked List: Sparse Matrix

1

2

3

4

5

6

1 2 3 4 5

* *

* X * * M

* * * * *

P

*

K

A * *

* * * O

* L * *

C * * B

R ow

C olum n

29.07.09 IT 60101: Lecture #2 25

Application of Linked List: Sparse MatrixHEADER

6 5 0 1 0 2CH1 CH2 CH3 CH4 CH5

RH1

RH2

RH3

RH4

RH5

RH6

1 0

2 0

3 0

4 0

5 0

6 0

4 1

5 3

4 5

2 22 5

1 3

6 1 6 2K C B

L

P

A

XM

O

6 5

Points to HEADER

29.07.09 IT 60101: Lecture #2 26

Application of Linked List: Polynomial

P(x) = anxen + an–1xen–1 + · · · + a1xe1

CO EFF EXPO LINK

3 8 -7 6 14 3 10 1 -5 0

P(x) = 3x8 – 7x6 + 14x3 + 10x – 5

29.07.09 IT 60101: Lecture #2 27

Application of Linked List: Polynomial

• For polynomial manipulation

See the book

Classic Data StructuresChapter 3

PHI, 2nd Edn., 17th Reprint

29.07.09 IT 60101: Lecture #2 28

Application of Linked List: Memory

• For memory management

See the book

Classic Data StructuresChapter 3

PHI, 2nd Edn., 17th Reprint


Recommended