LIMITATION OF STATIC
MEMORY ALLOCATION
Size
◦ E.g. int a [10];
Space
10 20 30 ? ?
18
In a sequence using an array, inserting a new
item needs to move others back...
10 20 20 30 ?
18
In a sequence using an array, inserting a new
item needs to move others back...
10 18 20 30 ?
So the Big-O of the insert is O(n)
How can we insert a new item without moving
others ?
10 20 30 ? ?
18
10 20 30 ? ?
18
10 20 30 ? ?
18
10 20 30 ? ?
18
Examples:
List of ten students marks
int studentMarks[10];
in run-time:
◦ malloc()
◦ alloc()
◦ Calloc()
The malloc() can be used to allocate
a block memory
It reverse a specified size of memory
& return pointer of type void
Syntax:
◦ ptr=(cast_type *) malloc (byte_size);
The malloc() returns a pointer
of cast type to an area of memory
with size of byte_size
e.g.:
◦ y=(int *) malloc (100*sizeof(int));
Each node in the linked list is a
class, as shown here.
15 data
class node
{
public: link
typedef int value_type; 10 data
...
private:
value_type data; link 7 data
node *link;
}; null
link
The data portion of each node is
a type called value_type, defined
by a typedef. 15 data
class node
{
public: link
typedef int value_type; 10 data
...
private:
value_type data; link 7 data
node *link;
}; null
link
Each node also contains a link
field which is a pointer to
another node. 15 data
class node
{
public: link
typedef int value_type; 10 data
...
private:
value_type data; link 7 data
node *link;
}; null
link
A program can keep track of
the first node by using a
pointer variable such as 15 data
head_ptr
null
link
node * head_ptr;
A program can also keep track
of the last node by using a
pointer variable such as tail_ptr 15 data
in this example.
Notice that tail_ptr itself is not link
10
a node -- it is a pointer data
to a node.
link 7 data
head_ptr
null
link
node * head_ptr;
tail_ptr
node * tail_ptr;
A program can keep track of
the first and the last nodes by node * head_ptr;
using pointer variables such as node * tail_ptr;
head_ptr, tail_ptr. head_ptr = NULL;
Notice that neither head_ptr tail_ptr = NULL;
nor tail_ptr is a node -- it is a // NULL can be
pointer to a node. used for any
For an empty list, null is stored
pointers!
in both the head and the tail
pointers.
null null
head_ptr tail_ptr
The node class is fundamental to linked lists
The private member variables
◦ data: a value_type variable
◦ link: a pointer to the next node
The member functions include:
◦ A constructor
◦ Set data and set link
◦ Retrieve data and retrieve link
class node
{ default argument given
public: by the value_type
// TYPEDEF
typedef double value_type;
default constructor (no
The node class is fundamental
// CONSTRUCTOR to DC for int and double in
linked lists
old versions of C++)
The private member
node(
variables
const value_type& init_data = value_type( ),
◦ data_field node* init_link = NULL
)
◦ link_field { data = init_data; link = init_link; }
The member functions
// Member include:
functions to set the data and link fields:
◦ A constructor
void set_data(const value_type& new_data) { data = new_data; }
void set_link(node* new_link) { link = new_link; }
◦ Set data and set link
// Constant member function to retrieve the current data:
◦ Retrieve data anddata(
value_type retrieve link
) const { return data; }
link 7 data
head_ptr
null
link
Design Container Classes using Linked Lists
◦ The use of a linked list is similar to our previous
use of an array in a container class
◦ But storing and retrieving needs more work since
we do not have that handy indexing
=> Linked List Toolbox
◦ using node class
This lecture will show four functions:
◦ Compute the length of a linked list (code)
◦ Insert a new node at the head (code)
◦ Insert a node at any location (pseudo-code)
◦ Delete a node from the head (pseudo-code)
Read Section 5.2 for other functions in the
Toolbox
◦ will be used in container classes bag and
sequence
size_t list_length(const node* head_ptr);
7
3 null
count head_ptr
cursor
size_t list_length(const node* head_ptr)
{
const node *cursor;
size_t count = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++;
return count;
}
0 null
count head_ptr
This lecture will show four functions:
◦ Compute the length of a linked list (code)
◦ Insert a new node at the head (code)
◦ Insert a node at any location (pseudo-code)
◦ Delete a node from the head (pseudo-code)
Read Section 5.2 for other functions in the
Toolbox
◦ will be used in container classes bag and
sequence
void list_head_insert(node*& head_ptr, const node::value_type& entry);
variable insert_ptr.
1
5
10
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
1
5
10
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
What expression 1
appears on the left 5
side of the assignment 10
statement ?
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Instead, Set_data 1
function is used since 5
data_field is a private 10
variable of the node
7
class 13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
head_ptr is pointing 7
13
to. entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
7
13
entry
null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
13 null
entry
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr
13 null 13
entry
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr
13 null 13
entry null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr
13 13
entry null
head_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
When the function
insert_ptr = new node; returns, the linked list
insert_ptr->set_data(entry); has one node,
insert_ptr->set_link(head_ptr);
containing 13.
head_ptr = insert_ptr;
}
13
head_ptr null
Always make sure
that your linked
list functions work
correctly with an
empty list.
EMPTY LIST
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
head_ptr = insert_ptr;
}
AApointer
pointer
to
tothe
the
head
headof of
the
thelist
list
Determine whether the new node will be the first node
in the linked list. If so, then there is only one step:
list_head_insert(head_ptr, entry);
TTh
inin hee
tht dda
hee atat
nne at
eww otop
nno puut
odd t
ee
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
In
Inthis
thisexample,
example, the
the 1
new previous_ptr
newnode
nodewill
willbe
be 0
the
thesecond
secondnode
node 15
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
Look
Lookatatthethepointer
pointer 1
which previous_ptr
whichisis in
inthe
thenode
node 0
*previous_ptr
*previous_ptr 15
7
What is the name of this
null
pointer ? head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
This 1
Thispointer
pointer isiscalled
called previous_ptr 0
previous_ptr->link
previous_ptr->link
15
7
Always remember how
null
can you access link head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
previous_ptr->link
previous_ptr->link
points 1
pointstotothe
thehead
head previous_ptr 0
of
ofaasmaller
smaller linked
linked
list, 15
list,with
with 10
10and
and77
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position. 13
The
Thenew
newnodenodemust must
be 1
beinserted
inserted at at the
the previous_ptr 0
head
headof ofthis
thissmall
small
linked 15
linkedlist.
list.
7
Write one C++ statement
which will do the insertion. null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position. 13
X
1
list_head_insert(previous_ptr->link, entry); previous_ptr 0
15
?!!!!
blele?
7
Write one C++ statement
riaab
vaari
head_ptr
ivaat
riv
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
13
sl_head_ptr
node *sl_head_ptr;
sl_head_ptr = previous_ptr->link();
list_head_insert(sl_head_ptr, entry); 1
previous_ptr->set_link(sl_head_ptr); previous_ptr 0
remove_ptr
13 10 1 7
5
null
head_ptr
Set up remove_ptr.
head_ptr = remove_ptr->link();
13 10 1 7
5
null
head_ptr
Set up remove_ptr.
head_ptr = remove_ptr->link();
remove_ptr
13 10 1 7
5
null
head_ptr
Set up remove_ptr.
head_ptr = remove_ptr->link;
delete remove_ptr; // Return the node's memory to heap.
remove_ptr
13 10 1 7
5
null
head_ptr
Here’s what the linked list looks like after the removal finishes.
10 1 7
5
null
head_ptr
It is easy to insert a node at the front of a
list.
The linked list toolkit also provides a
list.
The linked list toolkit also provides a
has
◦ set and retrieve functions
The functions in the Toolkit are not member
functions of the node class
◦ length, insert(2), remove(2), search, locate, copy,...
◦ compare their Big-Os with similar functions for an
array
They can be used in various container
classes, such as bag, sequence, etc.
Self-Test Exercises (node)
◦ 1-12
Read after class
◦ Linked List ToolKit (Section 5.2)
◦ Do Self-Test Ex 13 -25
Read before the next lecture
◦ Section 5.3- 5.4
Programming Assignment 4
◦ Detailed guidelines online!
Presentation copyright 1997, Addison Wesley Longman,
For use with Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright
Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club
Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using C++ are welcome
to use this presentation however they see fit, so long as this copyright notice remains
intact.