Você está na página 1de 24

Data Structures

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.)

A particular example of lists is array (array list)


Adjacent chunks of memory, where elements
are accessed directly through integer indices
b e a d a
0 1 2 3 4
CT077-3-2-DSTR

Data Structures

Introduction
Another example of lists is Linked Lists

Why need
one?

Array lists have advantages


Fast random access to any element

but also have disadvantages


Array size is fixed (changing it is costly)
Insertion of new elements and deleting existing
elements are slow operations (unless happening at
the end)
CT077-3-2-DSTR

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

Head (or first) address allows access to list


elements
Address of the first node in the list

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

Linked list and values of the links

Most of the time, we just draw arrows to indicate


pointers pointing at memory locations, since the
exact address values are not of high importance

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;
};

Or a struct can be used similarly


Data Structures

Linked Lists: Some Properties


A head variable is declared as
NodeType *
and it storeshead;
address of first node in the list

For each node


Info stores information
Link stores address of next node

For example, we can have the following list

CT077-3-2-DSTR

Data Structures

Linked Lists: Some Properties

CT077-3-2-DSTR

Data Structures

10

Linked Lists: Some Properties


NodeType * current = head;
Copies value of head into current

Value
current

2000

current->info

17

current->link
current->link->info
CT077-3-2-DSTR

2800
92
Data Structures

11

Linked Lists: Some Properties


current = current->link;
Copies value of current->link (2800)
into current

Value
current

2800

current->info

92
1500

current->link

63

current->link->info
CT077-3-2-DSTR

Data Structures

12

Linked Lists: Some Properties

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

0 (that is, NULL)

current>link->link->link

Does not exist

current>link->link->link->info Data Structures

gives runtime error


13

Traversing a Linked List


The basic operations of a linked list are:

Determine the size of the list


Insert an item in the list
Search to determine if an item is in the list
Accessing list elements (get and set element at index i)
Delete an item from the list

These operations require list traversal


Given a pointer to the first node of the list, step through
the nodes of the list
List size can be obtained without traversal, by modifying
the data structure (more on this later)
CT077-3-2-DSTR

Data Structures

14

Traversing a Linked List


To traverse a linked list:

Example, printing lists elements:

CT077-3-2-DSTR

Data Structures

15

Getting Linked List Size


Using traversal, how to calculate the number of
elements?
current = head;
int size = 0;
while(current != NULL){
size++;
current = current->link;
}

Is there a better way to know linked lists size


other than scanning the whole list everytime?

CT077-3-2-DSTR

Data Structures

16

Create a LinkedList Structure


Create a size field (data member) encapsulated in the
LinkedList data structure (initialized to zero)
Update the field upon insertion and deletion (+1 or -1)
class LinkedList {
public:
NodeType* head;
int size;
int getSize(){
int size = 0;
NodeType * current = head;
while(current != NULL){
size++;
current = current->link;
}
return size;
}
};

CT077-3-2-DSTR

Data Structures

This is a simple example


of how a data structure
and
an
algorithm
cooperate to implement
efficient solutions.
We traded off extra small
memory (not necessarily
always small) to gain
speed in calculating the
size of the linked list.

17

Inserting a New Element


We can insert at the beginning of the list, at the
end of it, or at given position
LinkedList(){
this->size = 0;
this->head= NULL;
}

class NodeType {
public:
int info;
NodeType* link;
};

void insertAtBeginning(int value){


NodeType * newNode= new NodeType;
newNode->info = value;
newNode->link = head;
head = newNode;
size++;
}

class LinkedList {
public:
NodeType* head;
int size;

// .. the other implemented methods


};
CT077-3-2-DSTR

Data Structures

18

Inserting a New Element


Draw a diagram representing what happens in
the memory of:
void main(){
LinkedList l1;
LinkedList l2;
l1.insertAtBeginning(5);
l1.insertAtBeginning(9);
l1.insertAtBeginning(3);
cout << l1.getSize() << endl;
l2.insertAtBeginning(9);
l2.insertAtBeginning(7);
cout << l2.getSize() << endl;
}
CT077-3-2-DSTR

Data Structures

19

Why the New Node Should be in


the Heap?
What happens if we change the implementation
of insert so that new node is on the stack?
void insertAtBeginning(int
value){
NodeType newNode;
newNode.info = value;
newNode.link = head;
head = & newNode;
size++;
}
void main(){
LinkedList list;

The object newNode will be


deleted from memory after
insertAtBeginning function
ends, and therefore the
head of the list will be a
dangling pointer.

list.insertAtBeginning(5);
cout << list.head->info;
}
CT077-3-2-DSTR

Data Structures

20

Inserting a New Element at the End


Write the code that navigates until the last node,
and then inserts the new node there.
void insertAtEnd(int value){
NodeType * newNode= new NodeType;
newNode->info = value;
newNode->link = NULL;
if (head == NULL)
head = newNode;
else{
NodeType * last = head;
while(last->link != NULL)
last= last->link;
last->link = newNode;
}
size++;
}
CT077-3-2-DSTR

Data Structures

Create and setup


the new node
If the list is empty
The general case
requires finding
the last node in
the list and make
its link point at
the new node
21

Clearing a Linked List


Sometimes you fill up a list, and want to empty it so that it
can be filled up again with new elements

void clear (){


NodeType * current = head;
while(head != NULL){
current = current->link;
delete head;
head = current;
}
size = 0;
}

CT077-3-2-DSTR

Data Structures

22

Proper Cleaning of the Memory


When the dynamically created nodes should be deleted?
Implement the destructor to delete all the nodes when the
list object is deleted
What is the output of the following main program?
~LinkedList(){
cout <<"Going to delete all "<< size
<<" elements of the list."<< endl;
NodeType * current = head;
while(head != NULL){
current = current->link;
delete head;
head = current;
}
}

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

Você também pode gostar