Sunteți pe pagina 1din 13

Linked Lists:

Introduction
head

node

node

node

node

node

tail

3.1
Structuring Data and Building
Algorithms
Structuring Data: Linked Lists
Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (1)

Linked Lists
vs Arrays, Records
Item
data
connected?
memory

arrays
no

array of records
yes

linked lists
yes

contiguous

contiguous

non-contiguous

access

random

random

sequential

size

fixed

fixed

dynamic

ex. 1

int time[30];
float distance[30];

struct P{
int time;
float distance;
};
struct P point[30];

struct P{
int time;
float distance;
struct P *ptr;
};

ex. 2

char name[800][40];
long studID[800];
float mark[800];
char letterGrade[800];

struct StudInfo{
char name[40];
long studID;
float mark;
char letterGrade;
};
struct StudInfo b[800];

struct StudList{
char name[40];
long studID;
float mark;
char letterGrade;
struct StudList *ptr;
};
.

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (2)

Notation
fundamental unit

array
element

linked list
node

beginning

first element

head

end

last element

tail

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (3)

Arrays, Records, Linked Lists


Hadhrami
Jin Leng
Naveena
Xiao Ming

Hadhrami
9800148
95
A

9800148
99153253
99138105
99126702

Jin Leng
99153253
72
B

95
72
55
15

Naveena
99138105
55
C

A
B
C
F

Xiao Ming
99136702
15
F

Arrays

Records

head

Naveena
99138105
55
C

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

Hadhrami
9800148
95
A

Jin Leng
99153253
72
B

Xiao Ming
99136702
15
F
Linked List

3.1 (4)

LL advantage (1)

Why should I use Linked Lists?


Array of records seems simpler it can also group
related information.
1st + dynamic nature
2nd + adding, deleting, sorting (no copy)

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (5)

Deletion
in Arrays
Hadhrami
Jin Leng
Naveena
Xiao Ming
9800148
99153253
99138105
99126702

LL advantage (2)

Hadhrami
Naveena
Xiao Ming
9800148
99138105
99126702

Hadhrami
Naveena
Xiao Ming
9800148
99138105

9800148
99138105
99126702

99126702

95
72
55
15

95

A
B
C
F

A
C
F

Original

Delete

Copy up

55
15

Hadhrami
Naveena
Xiao Ming

95
55

95
55
15

15
A
C

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

A
C
F
Copy up

3.1 (6)

Deletion
in Linked Lists
head

Naveena
99138105
55
C

Hadhrami
9800148
95
A

Jin Leng
99153253
72
B

LL advantage (3)

head

Naveena
99138105
55
C

Xiao Ming
99136702
15
F

Before
Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

Hadhrami
9800148
95
A

Jin Leng
99153253
72
B

Xiao Ming
99136702
15
F

After

3.1 (7)

Insertion
in Arrays
Hadhrami
Naveena
Xiao Ming

LL advantage (4)

Hadhrami
Naveena
Xiao Ming

9800148
99138105
99126702

9800148
99138105
99126702

95
55
15

Hadhrami
Naveena
Xiao Ming
9800148
99138105
99126702

95
55

A
C
F

55
15

A
C

Copy down

A
B
C
F

A
C
F

Copy down

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

9800148
971232
99138105
99126702
95
65
55
15

95

15

Hadhrami
Jon
Naveena
Xiao Ming

Insert

3.1 (8)

Insertion
in Linked Lists
head

Hadhrami
9800148
95
A

LL advantage (5)

head

Naveena
99138105
55
C

Naveena
99138105
55
C
Xiao Ming
99136702
15
F

Before
Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

Hadhrami
9800148
95
A

Jon
971232
65
B

Xiao Ming
99136702
15
F

After

3.1 (9)

Linked List
Advantages

LL advantage (6)

1st + dynamic nature of linked list


if array full cannot add any element
so must design array for biggest case

2nd + only need to copy new element


if array must copy following elements down,
if 800 students and add element at position 10,
790 x 4 = 3160 copies!

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (10)

Linked Lists
Disadvantages
Fast random access
slower sequential access
Memory usage slightly
less efficient
(need to 2 or 4 bytes to
hold the pointer)

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (11)

Glad you asked!


The Plan:
build a simple linked list
introduce other types of linked list
use a linked list to implement a stack
use a linked list to implement a queue
Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (12)

Next Up:

Viewgraphs 2001, 2006 by J. D. White. For use with Structuring data and building
algorithms: an ANSI-C approach by I. Chai and J. D. White (McGraw-Hill, 2006)

3.1 (13)

S-ar putea să vă placă și