Escolar Documentos
Profissional Documentos
Cultura Documentos
Linked Lists
(Part 1)
Objectives
By the end of this lesson, you will:
Learn about linked list data structure
Become aware of the basic properties of linked
lists
Be able to implement some of the basic
operations on linked lists
Discover how to create, manipulate, and use
linked list objects
CT077-3-2-DSTR
Data Structures
Introduction
A collection of items can be organized and
processed in memory using a list data structure
A list as an abstract type is a linear structure which
contains a sequence or elements (first element,
second element, last element, previous element, etc.)
Data Structures
Introduction
Another example of lists is Linked Lists
Why need
one?
Data Structures
Linked Lists
It is a series of connected nodes, where each
node is a data structure
Every node (except last)
Contains address of the next (following) node
Node components
Data: stores relevant information
Link: stores address
Structure of a node
CT077-3-2-DSTR
Data Structures
Linked Lists
An arrow points to the following nodes address
A pointer value stored in node
Down arrow in last node indicates NULL link field,
i.e. a pointer pointing at nothing
X mark is also used in diagrams instead of down arrow
Linked list
CT077-3-2-DSTR
Data Structures
Linked Lists
Example: a list containing two elements (45 and 65)
supposing the first node is at memory location
1200, and the second node is at memory
location 1575, linked list will look like this
0
CT077-3-2-DSTR
Data Structures
Linked Lists
Because each node of a linked list has two
components, we need to declare each node as a
class (or struct)
Data type (or info) of a node depends on the
specific application (linked list of what?)
The link component of each node is a pointer
class NodeType {
public:
int info;
NodeType*
link;
};
CT077-3-2-DSTR
struct NodeType {
int info;
NodeType* link;
};
CT077-3-2-DSTR
Data Structures
CT077-3-2-DSTR
Data Structures
10
Value
current
2000
current->info
17
current->link
current->link->info
CT077-3-2-DSTR
2800
92
Data Structures
11
Value
current
2800
current->info
92
1500
current->link
63
current->link->info
CT077-3-2-DSTR
Data Structures
12
Value
head->link->link
1500
head->link->link->info
63
head->link->link->link
head->link->link->link->info
CT077-3-2-DSTR
3600
45
3600
current>link->link
45
current->link->link->info
current>link->link->link
Data Structures
14
CT077-3-2-DSTR
Data Structures
15
CT077-3-2-DSTR
Data Structures
16
CT077-3-2-DSTR
Data Structures
17
class NodeType {
public:
int info;
NodeType* link;
};
class LinkedList {
public:
NodeType* head;
int size;
Data Structures
18
Data Structures
19
list.insertAtBeginning(5);
cout << list.head->info;
}
CT077-3-2-DSTR
Data Structures
20
Data Structures
CT077-3-2-DSTR
Data Structures
22
CT077-3-2-DSTR
Data Structures
void main(){
if (true) {
LinkedList list;
list.insertAtBeginning(5);
list.insertAtBeginning(9);
}
LinkedList list2;
list2.insertAtBeginning(3);
}
23
CT077-3-2-DSTR
Data Structures
24