You are on page 1of 23

Chapter 4

Linked Lists
Memory Allocation
There are essentially two types of memory allocation
Static – Done by the compiler automatically (implicitly).
Global variables or objects -- memory is allocated at the start of
the program, and freed when program exits; alive throughout
program execution
 Can be access anywhere in the program.
Local variables (inside a routine) – memory is allocated when the
routine starts and freed when the routine returns.
 A local variable cannot be accessed from another routine.
Allocation and free are done implicitly.
No need to explicitly manage memory is nice (easy to work with),
but has limitations!
Using static allocation, the array size must be fixed.
Consider the grade roster for the class? What is the number of people in the
class?
Cont..
There are essentially two types of memory
allocation
Wouldn’t it be nice to be able to have an array
whose size can be adjusted depending on needs.
 Dynamic memory allocation deals with this situation.

Dynamic – Done explicitly by programmer.


Programmer explicitly requests the system to allocate
memory and return starting address of memory
allocated . This address can be used by the
programmer to access the allocated memory.
When done using memory, it must be explicitly freed.
Explicitly allocating memory in C++:
The ‘new’ Operator
Used to dynamically allocate memory
Can be used to allocate a single
variable/object or an array of
variables/objects
The new operator returns pointer to the
type allocated
Examples:
◦ char *my_char_ptr = new char;
◦ int *my_int_array =new int[20];
Explicitly freeing memory in C++: the
‘delete’ Operator
Used to free memory allocated with new operator
The delete operator should be called on a pointer
to dynamically allocated memory when it is no
longer needed
Can delete a single variable/object or an array
delete PointerName;
delete [] ArrayName;
After delete is called on a memory region, that
region should no longer be accessed by the
program
Why use dynamic memory allocation?
Allows data (especially arrays) to take on
variable sizes (e.g. ask the user how many
numbers to store, then generate an array
of integers exactly that size).
Allows locally created variables to live
past end of routine.
Allows us to create many structures used
in Data Structures and Algorithms
The composition of a Linked List
A linked list is called "linked" because
each node in the series has a pointer that
points to the next node in the list.
Cont..
SinglyLinked Lists
Doubly Linked Lists
Cont..
A linked data structure consists of items
that are linked to other items
◦ How? each item points to another item

Singly linked list: each item points to the


next item
Doubly linked list: each item points to the
next item and to the previous item
Advantage of linked list
The items do not have to be stored in
consecutive memory locations: the
successor can be anywhere physically
◦ So, can insert and delete items without shifting
data
◦ Can increase the size of the data structure
easily
Linked lists can grow dynamically (i.e. at
run time) – the amount of memory space
allocated can grow and shrink as needed
Nodes
A linked list is an ordered sequence of items
called nodes
◦ A node is the basic unit of representation in a linked list
A node in a singly linked list consists of two
fields:
◦ A data portion
◦ A link (pointer) to the next node in the structure
The first item (node) in the linked list is accessed
via a front or head pointer
◦ The linked list is defined by its head (this is its starting
point)

4-
11
Singly Linked List
head pointer "defines" the linked list
head (note that it is not a node)

these are nodes

data data data .

4-
12
Linked List
Note: we will hereafter refer to a singly linked list just as a
“linked list”

Traversing the linked list


◦ How is the first item accessed?
◦ The second?
◦ The last?

What does the last item point to?


◦ We call this the null link

4-
13
Linked List Operations
We will now examine linked list operations:
Add an item to the linked list
◦ We have 3 situations to consider:
 insert a node at the front
 insert a node in the middle
 insert a node at the end
Delete an item from the linked list
◦ We have 3 situations to consider:
 delete the node at the front
 delete an interior node
 delete the last node

4-
14
Inserting a Node at the Front
node
node points to the new node to be
inserted, front points to the first node
of the linked list
front

node
1. Make the new node point to the
first node (i.e. the node that front
points to)
front

4-
15
node
2. Make front point to the new node
(i.e the node that node points to)
front

4-
16
Inserting a Node in the Middle
node
Let's insert the new node after the third
node in the linked list

front insertion point

1. Locate the node preceding the


insertion point , since it will have to be node
modified (make current point to it)

front

current
4-
17
2. Make the new node point to the node
after the insertion point (i.e. the node node
pointed to by the node that current
points to)
front

current

node
3. Make the node pointed to by current
point to the new node

front
X
current
4-
18
Deleting the First Node
front points to the first node in the linked list,
which points to the second node

front

Make front point to the second node (i.e. the node


pointed to by the first node)

front
X

4-
19
Deleting
front
an Interior Node

previous current

1. Traverse the linked list so that current points to the node to be


deleted and previous points to the node prior to the one to be
deleted

front

previous current

2. We need to get at the node following the one to be


deleted (i.e. the node pointed to by the node that current
points to) 4-
20
front
X

previous current

3. Make the node that previous points to, point to the


node following the one to be deleted

4-
21
Doubly Linked Lists
In a doubly linked list, each node has two
links:
◦ A reference to the next node in the list
◦ A reference to the previous node in the list
 What is the “previous” reference of the first node in
the list?

4-
22
Doubly Linked List

head tail

. .

4-
23

You might also like