Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 5
Linked Lists
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
passing by value
passing by reference
implicit: arrays, strings
explicit: pointers
pass ???
9
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
15
16
Question
What are some disadvantages of arrays?
not dynamic
size is fixed once created
you can resize it, but you have to do it by hand
same for insertion & deletion; its possible, but its
difficult and theres no built-in function for it
Question
Can we fix these with linked lists? How?
not dynamic
linked lists can change size constantly
can add nodes anywhere in a linked lists
elements can be removed with no empty spaces
Question
Are there any disadvantages to linked lists?
harder to search/access than arrayz
need to manage size/counter for size
harder to manage memory
in-list cycles, segfaults, etc.
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
21
data
data
data
data
next
next
next
next
NULL
22
Nodes
a node is one element of a linked list
nodes consist of two parts:
data
next
23
Nodes
a node is one element of a linked list
nodes consist of two parts:
data stored in node
data
next
24
Nodes
a node is one element of a linked list
nodes consist of two parts:
data stored in node
pointer to next node in list
data
next
25
Node Definition
nodes are typically represented as structs
typedef struct node {
int
data;
NODEPTR next;
} NODE;
26
Node Definition
nodes are typically represented as structs
typedef struct node * NODEPTR;
typedef struct node {
int
data;
NODEPTR next;
} NODE;
typedef NODEPTR beforehand so that it can be
used in the definition of the NODE structure
27
data
data
data
data
next
next
next
next
NULL
28
List Head
points to the first NODE in the list
headPtr
29
Linked Lists
headPtr
NODEPTR
data
data
data
data
next
next
next
next
NODE
NODE
NODE
NODE
NULL
30
Linked Lists
headPtr
NODEPTR
@ 0xFFC0
data
data
data
data
next
next
next
next
NODE
@ 0xFFC4
NODE
@ 0xFFC8
NODE
@ 0xFFDC
NODE
@ 0xFFEE
NULL
31
Linked Lists
0xFFC4
NODEPTR
@ 0xFFC0
data
data
data
data
0xFFC8
0xFFDC
0xFFEE
NULL
NODE
@ 0xFFC4
NODE
@ 0xFFC8
NODE
@ 0xFFDC
NODE
@ 0xFFEE
NULL
32
Linked Lists
0xFFC4
NODEPTR
@ 0xFFC0
data
= 5;
data
= 3;
data
= 8;
data
= 2;
0xFFC8
0xFFDC
0xFFEE
NULL
NODE
@ 0xFFC4
NODE
@ 0xFFC8
NODE
@ 0xFFDC
NODE
@ 0xFFEE
NULL
33
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
34
35
0xFFC4
0xDC44
address
where its
stored in
memory
value
where it
points to in
memory
36
Passing Pointers
when we pass a pointer by value, we pass
where it points to in memory
37
38
39
Passing Pointers
when we pass a pointer by reference, we are
passing where it is stored in memory
so we can change both
where it points to in memory
and
the values that are stored there
41
42
43
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
46
47
headPtr
NODEPTR
NULL
48
headPtr
NODEPTR
NULL
data
tempNode
next
NULL
NODE
49
NULL
data
= 17;
tempNode
next
1. declare a headPtr,
and set equal to NULL
2. allocate space for a
node and set to a
temporary variable
3. initialize nodes data
NULL
NODE
50
data
= 17;
next
NODE
tempNode
NULL
1. declare a headPtr,
and set equal to NULL
2. allocate space for a
node and set to a
temporary variable
3. initialize nodes data
4. insert node in list
51
data
= 17;
data
= 2;
next
next
NODE
NODE
NULL
tempNode
52
headPtr
NODEPTR
data
= 17;
data
= 2;
data
= 9;
next
next
next
NODE
NODE
NODE
NULL
tempNode
53
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
54
Creating a Node
NODEPTR CreateNode (void)
55
Creating a Node
NODEPTR CreateNode (void)
56
Creating a Node
NODEPTR CreateNode (void)
temp->data = data;
(*temp).data = data;
58
NODE
STRUCT
int classNum;
char room[ROOM_STR];
char title [TITLE_STR];
CIS_CLASS
STRUCT
(data)
next
59
temp->class.classNum = classNum;
strcpy(temp->class.room, room);
strcpy(temp->class.title, title);
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
61
set curr to
*headPtr, the first
NODE in the list
headPtr
NODEPTR
prev
NULL
data
data
data
next
next
next
NODE
NODE
NODE
NULL
64
headPtr
NODEPTR
prev
NULL
if it doesnt, continue
data
data
data
next
next
next
NODE
NODE
NODE
NULL
65
headPtr
NODEPTR
prev
NULL
if it doesnt, continue
data
data
data
next
next
next
NODE
NODE
NODE
NULL
or if your conditions have been met! but you
must always check that curr != NULL first
66
curr
headPtr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
67
curr
headPtr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
68
curr
headPtr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
69
curr
headPtr
NODEPTR
continue to repeat
steps 2-4 until you
reach the end
data
data
data
next
next
next
NODE
NODE
NODE
NULL
70
curr
headPtr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
71
curr
headPtr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
72
curr
headPtr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
73
headPtr
curr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
74
prev
headPtr
curr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
if it doesnt,
continue
NULL
75
prev
curr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
76
prev
curr
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
77
curr
prev
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
78
curr
prev
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
NULL
79
curr
prev
NODEPTR
data
data
data
next
next
next
NODE
NODE
NODE
it does!
weve
reached the
end of the
list
NULL
80
81
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
82
Inserting a Node
void Insert (NODEPTR *headPtr,
NODEPTR temp)
83
Inserting a Node
check if curr == NULL
(end of list)
headPtr
prev
curr
NODEPTR
data
next
NODE
NULL
84
Inserting a Node
headPtr
prev
curr
NODEPTR
data
next
NODE
NULL
85
Inserting a Node
headPtr
prev
curr
NODEPTR
data
data
next
next
NODE
NODE
NULL
86
Inserting a Node
headPtr
prev
curr
NODEPTR
data
data
next
next
NODE
NODE
NULL
new node is
successfully inserted
at end of the list!
87
curr
data
data
next
next
NODE
NODE
temp
data
next
NULL
NODE
89
curr
data
data
next
next
NODE
NODE
temp->next = curr;
temp
data
next
NODE
90
curr
data
data
next
next
NODE
NODE
prev->next = temp;
temp
data
next
NODE
91
curr
data
data
next
next
NODE
NODE
temp
temp is now
stored in the list
between prev
and curr
data
next
NODE
92
temp
curr
data
data
data
next
next
next
NODE
NODE
NODE
temp is now
stored in the list
between prev
and curr
return a
successful code
(insert worked)
93
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
94
Deleting a Node
int Delete (NODEPTR *headPtr,
int target)
curr
data
data
data
next
next
next
NODEPTR
NODEPTR
NODEPTR
96
curr
data
data
data
next
next
next
NODEPTR
NODEPTR
NODEPTR
97
curr
data
data
data
next
next
next
NODEPTR
NODEPTR
NODEPTR
remove curr
from the list by
setting
prev->next to
curr->next
98
curr
data
data
data
next
next
next
NODEPTR
NODEPTR
NODEPTR
free the
memory used
by curr and set
pointers to NULL
99
curr
data
data
data
next
next
next
NODEPTR
NODEPTR
NODEPTR
free the
memory used
by curr and set
pointers to NULL
curr->next = NULL;
NULL
100
curr
data
data
data
next
next
next
NODEPTR
NODEPTR
NODEPTR
NULL
free the
memory used
by curr and set
pointers to NULL
curr->next = NULL;
free(curr);
101
curr
data
data
next
next
NODEPTR
NODEPTR
free the
memory used
by curr and set
pointers to NULL
curr->next = NULL;
free(curr);
102
data
curr
NULL
data
next
next
NODEPTR
NODEPTR
free the
memory used
by curr and set
pointers to NULL
curr->next = NULL;
free(curr);
curr = NULL;
103
data
curr
NULL
data
next
next
NODEPTR
NODEPTR
free the
memory used
by curr and set
pointers to NULL
curr->next = NULL;
free(curr);
curr = NULL;
104
Outline
(from last class) Memory and Functions
Linked Lists & Arrays
Anatomy of a Linked List
Details On Handling headPtr
Homework 4B
105
Homework 4B
Karaoke
heavy on pointers and memory management
think before you attack
start early
test often (dont forget edge cases)
use a debugger when needed
106
107