Foundation of Computing Systems

Download Report

Transcript Foundation of Computing Systems

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

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 59 N1 14 N4 38 N2 72 N5 64 N3 80 N6 Header Memory Location 41 42 43 44 45 46 47 48 49 50 .

.

.

DATA 38 14 59 80 72 64 .

.

.

LINK 50 47 41 45 43 .

.

.

Array of pointers 29.07.09

IT 60101: Lecture #2 4

Linked List Representation: Dynamic

AVAIL NEW XY X X HEADER IT 60101: Lecture #2 XY 29.07.09

5

Linked List Representation: Dynamic

AVAIL HEADER X IT 60101: Lecture #2 29.07.09

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 HEADER 2 X From memory bank

X

1 29.07.09

IT 60101: Lecture #2 9

Single Linked List: Insert at End

new HEADER

X

ptr 1 29.07.09

IT 60101: Lecture #2 10

Single Linked List: Insert at Any Place

new 29.07.09

HEADER

X

ptr 2 KEY X 1 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 ptr 1 KEY X ptr1 29.07.09

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 29.07.09

ptr1 ptr 1 X KEY Return to the memory bank IT 60101: Lecture #2 15

29.07.09

Single Linked List: Copy

L1 HEADER1 L2 HEADER2 X . . .

To memory bank . . .

IT 60101: Lecture #2 16

HEADER

Circular Linked List

29.07.09

IT 60101: Lecture #2 17

Merging Two Circular Linked Lists

29.07.09

X HEADER1 2 X HEADER2 1 ptr1 ptr2 Return to memory . . .

. . .

IT 60101: Lecture #2 ptr2 18

29.07.09

Double Linked List

LLINK DATA RLINK . . .

IT 60101: Lecture #2 19

29.07.09

Double Linked List: Insertion

NEW HEADER     ptr . . .

a) Insertion at the front ptr HEADER . . .

  b) Insertion at the end NEW ptr     NEW IT 60101: Lecture #2 c) Insertion at any intermediate position 20

29.07.09

Double Linked List: Deletion

ptr HEADER  ptr 1 X . . .

X  HEADER Return to the memory bank a) Deletion at the front ptr1  X . . .

ptr HEADER b) Deletion at the end ptr1 X ptr  KEY  X ptr2 Return to the memory bank 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

i

DATA

j

ROWLINK COLLINK 29.07.09

IT 60101: Lecture #2 23

Application of Linked List: Sparse Matrix

Row 1 2 3 4 5 6 Column 1 2 3 4 5 * *

A

* * * *

X

* *

M

* * * * *

P

* * *

O K

*

L

* *

C

* *

B

29.07.09

IT 60101: Lecture #2 24

Application of Linked List: Sparse Matrix

HEADER 0 CH1 1 0 CH2 2 CH3 CH4 CH5 6 5 RH1 1 0 RH2 2 0 RH3 3 0 RH4 4 0 RH5 5 0 4 1

P

29.07.09

RH6 6 0 6

K

1 Points to HEADER 2 2

X

1

A

3 5 3

L

6

C

2 IT 60101: Lecture #2 2

M

5 4 5

O

6

B

5 25

Application of Linked List: Polynomial

P

(

x

) =

a n x en

+

a n

–1

x en

–1 + · · · +

a

1

x e

1 C O E F F E X P O LIN K 29.07.09

3 8 -7 6 14 3 10 1

P

(

x

) = 3

x

8 – 7

x

6 + 14

x

3 + 10

x

– 5 -5 0 IT 60101: Lecture #2 26

Application of Linked List: Polynomial

• For polynomial manipulation See the book

Classic Data Structures Chapter 3

PHI, 2 nd Edn., 17 th 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, 2 nd Edn., 17 th Reprint 29.07.09

IT 60101: Lecture #2 28