Single Linked Lists - Curso de Ciencias de la Computación

SantiagoReyGoitia 6 views 69 slides Sep 03, 2024
Slide 1
Slide 1 of 69
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69

About This Presentation

Apuntes curso CS50, Single linked lists


Slide Content

Singly-Linked Lists

Singly-Linked Lists
•So far in the course, we’ve only had one kind of data structure
for representing collections of like values.
•structs, recall, give us “containers” for holding variables of different
data types, typically.
•Arrays are great for element lookup, but unless we want to
insert at the very end of the array, inserting elements is quite
costly –remember insertion sort?

Singly-Linked Lists
•Arrays also suffer from a great inflexibility –what happens if
we need a larger array than we thought?
•Through clever use of pointers, dynamic memory allocation,
and structs, we can put those two pieces together to
develop a new kind of data structure that gives us the ability to
grow and shrink a collection of like values to fit our needs.

Singly-Linked Lists
•We call this combination of elements, when used in this way, a
linked list.
•A linked list nodeis a special kind of structwith two members:
•Data of some data type (int, char, float…)
•A pointer to another node of the same type
•In this way, a set of nodes together can be thought of as
forming a chain of elements that we can follow from beginning
to end.

Singly-Linked Lists
typedefstructsllist
{
VALUE val;
structsllist* next;
}
sllnode;

Singly-Linked Lists
typedefstructsllist
{
VALUEval;
structsllist* next;
}
sllnode;

Singly-Linked Lists
typedefstructsllist
{
VALUE val;
structsllist* next;
}
sllnode;

Singly-Linked Lists
typedefstructsllist
{
VALUE val;
structsllist* next;
}
sllnode;

Singly-Linked Lists
•In order to work with linked lists effectively, there are a
number of operations that we need to understand:
1.Create a linked list when it doesn’t already exist.
2.Search through a linked list to find an element.
3.Insert a new node into the linked list.
4.Delete a single element from a linked list.
5.Delete an entire linked list.

Singly-Linked Lists
•Create a linked list.
sllnode* create(VALUE val);

Singly-Linked Lists
•Create a linked list.
sllnode* create(VALUE val);
•Steps involved:
a.Dynamically allocate space for a new sllnode.
b.Check to make sure we didn’t run out of memory.
c.Initialize the node’s valfield.
d.Initialize the node’s nextfield.
e.Return a pointer to the newly created sllnode.

Singly-Linked Lists
sllnode* new = create(6);
a.Dynamically allocate space for a
new sllnode.
b.Check to make sure we didn’t run
out of memory.
c.Initialize the node’s valfield.
d.Initialize the node’s nextfield.
e.Return a pointer to the newly
created sllnode.

Singly-Linked Lists
sllnode* new = create(6);
a.Dynamically allocate space for a
new sllnode.
b.Check to make sure we didn’t run
out of memory.
c.Initialize the node’s valfield.
d.Initialize the node’s nextfield.
e.Return a pointer to the newly
created sllnode.

Singly-Linked Lists
sllnode* new = create(6);
a.Dynamically allocate space for a
new sllnode.
b.Check to make sure we didn’t run
out of memory.
c.Initialize the node’s valfield.
d.Initialize the node’s nextfield.
e.Return a pointer to the newly
created sllnode.
6

Singly-Linked Lists
sllnode* new = create(6);
a.Dynamically allocate space for a
new sllnode.
b.Check to make sure we didn’t run
out of memory.
c.Initialize the node’s valfield.
d.Initialize the node’s nextfield.
e.Return a pointer to the newly
created sllnode.
6

Singly-Linked Lists
sllnode* new = create(6);
a.Dynamically allocate space for a
new sllnode.
b.Check to make sure we didn’t run
out of memory.
c.Initialize the node’s valfield.
d.Initialize the node’s nextfield.
e.Return a pointer to the newly
created sllnode.
6
new

Singly-Linked Lists
•Search through a linked list to find an element.
bool find(sllnode* head, VALUE val);

Singly-Linked Lists
•Search through a linked list to find an element.
bool find(sllnode* head, VALUE val);
•Steps involved:
a.Create a traversal pointer pointing to the list’s head.
b.If the current node’s valfield is what we’re looking for, report
success.
c.If not, set the traversal pointer to the next pointer in the list and go
back to step b.
d.If you’ve reached the end of the list, report failure.

Singly-Linked Lists
bool exists = find(list, 6);
2 3 5 6 8
list

Singly-Linked Lists
bool exists = find(list, 6);
2 3 5 6 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 3 5 6 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 3 5 6 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 3 5 6 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 7 5 3 8
list

Singly-Linked Lists
bool exists = find(list, 6);
2 7 5 3 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 7 5 3 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 7 5 3 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 7 5 3 8
list
trav

Singly-Linked Lists
bool exists = find(list, 6);
2 7 5 3 8
list
trav

Singly-Linked Lists
•Insert a new node into the linked list.
sllnode* insert(sllnode* head, VALUE val);

Singly-Linked Lists
•Insert a new node into the linked list.
sllnode* insert(sllnode* head, VALUE val);
•Steps involved:
a.Dynamically allocate space for a new sllnode.
b.Check to make sure we didn’t run out of memory.
c.Populate and insert the node at the beginning of the linked list.
d.Return a pointer to the new head of the linked list.

Singly-Linked Lists
•Insert a new node into the linked list.
sllnode* insert(sllnode* head, VALUE val);
•Steps involved:
a.Dynamically allocate space for a new sllnode.
b.Check to make sure we didn’t run out of memory.
c.Populate and insert the node at the beginning of the linked list.
d.Return a pointer to the new head of the linked list.

Singly-Linked Lists
list = insert(list, 12);
15 9 13 10
list

Singly-Linked Lists
list = insert(list, 12);
15 9 13 10
list
new

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
list
new

Singly-Linked Lists
•Decision time!
•Which pointer should we move first? Should the “12” node be
the new head of the linked list, since it now exists, or should
we connect it to the list first?
•This is one of the trickiest things with linked lists. Order
matters!

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
list
new

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
list
new

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
?
list
new

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
list
new

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
list
new

Singly-Linked Lists
list = insert(list, 12);
12 15 9 13 10
list
new

Singly-Linked Lists
•Delete an entire linked list.
void destroy(sllnode* head);

Singly-Linked Lists
•Delete an entire linked list.
void destroy(sllnode* head);
•Steps involved:
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.

Singly-Linked Lists
•Delete an entire linked list.
void destroy(sllnode* head);
•Steps involved:
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list list
list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list list
list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list list

Singly-Linked Lists
12 15 9 13 10
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list list

Singly-Linked Lists
12 15 9 13
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list

Singly-Linked Lists
12 15 9 13
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list list

Singly-Linked Lists
12 15 9
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list

Singly-Linked Lists
12 15 9
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list list

Singly-Linked Lists
12 15
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list

Singly-Linked Lists
12 15
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list list

Singly-Linked Lists
12
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list

Singly-Linked Lists
12
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);
list

Singly-Linked Lists
destroy()
destroy()
destroy()
destroy()
destroy()
destroy()
STACK FRAMES
a.If you’ve reached a null pointer, stop.
b.Delete the rest of the list.
c.Free the current node.
destroy(list);

Singly-Linked Lists
•In order to work with linked lists effectively, there are a
number of operations that we need to understand:
1.Create a linked list when it doesn’t already exist.
2.Search through a linked list to find an element.
3.Insert a new node into the linked list.
4.Delete a single element from a linked list.
5.Delete an entire linked list.
Tags