Single Linked Lists - Curso de Ciencias de la Computación
SantiagoReyGoitia
6 views
69 slides
Sep 03, 2024
Slide 1 of 69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
About This Presentation
Apuntes curso CS50, Single linked lists
Size: 688.42 KB
Language: en
Added: Sep 03, 2024
Slides: 69 pages
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
{
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
•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.