Linked Lists
Submitted By:-Mrs.Mehak Kapoor
Assistant Professor Computer Science & IT Dept.
Linked List
A linear collection of self- referential objects, called nodes, connected by other links
Declarations for Linked Lists
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
class node
{
public:
typedef double value_type;
...
private
value_type data_field;
node *link_field;
};
Declarations for Linked Lists
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
class node
{
public:
typedef int value_type;
...
private
value_type data_field;
node *link_field;
};
Declarations for Linked Lists
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
class node
{
public:
typedef int value_type;
...
private
value_type data_field;
node *link_field;
};
Declarations for Linked Lists
head_ptr
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
Declarations for Linked Lists
head_ptr
null
Inserting a Node at the Front
We want to add a new entry, 13, to the front of the linked list shown here.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
10
15
7
null
head_ptr
entry
13
Inserting a Node at the Front
10
15
7
null
head_ptr
entry
13
insert_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
10
15
7
null
head_ptr
entry
13
insert_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
10
15
7
null
head_ptr
entry
13
insert_ptr
13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
10
15
7
null
head_ptr
entry
13
insert_ptr
13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
The correct new node can be completely created in one step by calling an appropriate node constructor.
10
15
7
null
head_ptr
entry
13
insert_ptr
13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
10
15
7
null
head_ptr
entry
13
insert_ptr
13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
10
15
7
null
head_ptr
entry
13
insert_ptr
13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
10
15
7
null
head_ptr
13
When the function returns, the
linked list has a new node at the
front.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
Inserting a Node at the Front
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
Does the function work
correctly for the empty
list ?
Inserting a Node at the Front
head_ptr
entry
13
null
Does the function work
correctly for the empty
list ?
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
Inserting a Node at the Front
head_ptr
entry
13
null
insert_ptr
13
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
null
Inserting a Node at the Front
head_ptr
entry
13
insert_ptr
13
null
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
Inserting a Node at the Front
head_ptr
13
null
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
When the function
returns, the linked list
has one node.
Caution!
EMPTY LIST
Pseudocode for Inserting Nodes
Pseudocode for Inserting Nodes
list_head_insert(head_ptr, entry);
Pseudocode for Inserting Nodes
The function
we already wrote
list_head_insert(head_ptr, entry);
Pseudocode for Inserting Nodes
list_head_insert(head_ptr, entry);
A pointer
to the
head of
the list
Pseudocode for Inserting Nodes
list_head_insert(head_ptr, entry);
The data to put
in the new node
Pseudocode for Inserting Nodes
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
In this example, the
new node will be
the second node
previous_ptr
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Look at the pointer
which is in the node
*previous_ptr
previous_ptr
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
This pointer is called
previous_ptr->link_field
(although this name may
be private to the node)
previous_ptr
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
previous_ptr->link_field
points to the head
of a small linked
list, with 10 and 7
previous_ptr
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
The new node must
be inserted at the
front of this small
linked list.
13
previous_ptr
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
13
previous_ptr
list_head_insert(previous_ptr->link_field, entry);
Pseudocode for Inserting Nodes
Use a node member function to get the link field if needed.
15
10
7
null
head_ptr
13
previous_ptr
list_head_insert(previous_ptr->link( ), entry);
Pseudocode for Inserting Nodes
list_head_insert(head_ptr, entry);
list_head_insert(previous_ptr->link( ), entry);
Pseudocode for Inserting Nodes
Pseudocode for Removing Nodes
Removing the Head Node
10
15
7
null
head_ptr
13
remove_ptr
Removing the Head Node
10
15
7
null
head_ptr
13
remove_ptr
Draw the change that this statement will make to the linked list.
Removing the Head Node
10
15
7
null
head_ptr
13
remove_ptr
Removing the Head Node
10
15
7
null
head_ptr
13
remove_ptr
Removing the Head Node
Here’s what the linked list looks like after the removal finishes.
10
15
7
null
head_ptr
Summary
THANK YOU!