SlideShare a Scribd company logo
 This presentation shows how to
implement the most common
operations on linked lists.
Linked Lists in Action
 For this presentation, nodes in a
linked list are objects, as shown here.
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
 The data_field of each node is a type called
value_type, defined by a typedef.
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
 Each node also contains a link_field
which is a pointer to another node.
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
Declarations for Linked Lists
 A program can keep track of the front
node by using a pointer variable such
as head_ptr in this example.
 Notice that head_ptr is not a node -- it
is a pointer to a node.
head_ptr
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
Declarations for Linked Lists
 A program can keep track of the front
node by using a pointer variable such
as head_ptr.
 Notice that head_ptr is not a node -- it
is a pointer to a node.
 We represent the empty list by storing
null in the head pointer.
head_ptr
null
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
We want to add a new entry, 13,
to the front of the linked list
shown here.
10
15
7
null
head_ptr
entry
13
Inserting a Node at the Front
 Create a new node, pointed to
by a local variable insert_ptr.
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
insert_ptr = new node;
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
insert_ptr = new node;
Place the data in the new node's
data_field.
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
insert_ptr = new node;
Place the data in the new node's
data_field.
Connect the new node to the
front of the list.
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
insert_ptr = new node(entry, head_ptr);
The correct new node
can be completely
created in one step by
calling an appropriate
node constructor.
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
insert_ptr = new node(entry, head_ptr);
Make the old head pointer
point to the new node.
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
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
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);
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
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 ?
head_ptr
entry
13 null
Inserting a Node at the Front
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!
 Always make sure that
your linked list
functions work
correctly with an
empty list.
EMPTY LIST
Pseudocode for Inserting Nodes
 Nodes are often inserted at places other than the
front of a linked list.
 There is a general pseudocode that you can follow
for any insertion function. . .
Pseudocode for Inserting Nodes
 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);
Pseudocode for Inserting Nodes
 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);
Pseudocode for Inserting Nodes
 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);
A pointer
to the
head of
the list
Pseudocode for Inserting Nodes
 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);
Pseudocode for Inserting Nodes
 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.
Pseudocode for Inserting Nodes
15
10
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.
In this example, the
new node will be
the second node
previous_ptr
Pseudocode for Inserting Nodes
15
10
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
What is the name of
this orange pointer ?
Look at the pointer
which is in the node
*previous_ptr
previous_ptr
Pseudocode for Inserting Nodes
15
10
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
This pointer is called
previous_ptr->link_field
(although this name may
be private to the node)
What is the name of
this orange pointer ?
previous_ptr
Pseudocode for Inserting Nodes
15
10
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
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
 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.
The new node must
be inserted at the
front of this small
linked list.
13
Write one C++ statement
which will do the insertion.
previous_ptr
Pseudocode for Inserting Nodes
15
10
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
What might cause this
statement to fail to compile?
previous_ptr
list_head_insert(previous_ptr->link_field, entry);
Pseudocode for Inserting Nodes
15
10
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
Use a node member function
to get the link field if
needed.
previous_ptr
list_head_insert(previous_ptr->link( ), entry);
Pseudocode for Inserting Nodes
 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);
 Otherwise (if the new node will not be first):
 Set a pointer named previous_ptr to point to the node
which is just before the new node's position.
 Make the function call:
list_head_insert(previous_ptr->link( ), entry);
Pseudocode for Inserting Nodes
 The process of adding a new node in the middle
of a list can also be incorporated as a separate
function. This function is called list_insert in the
linked list toolkit of Section 5.2.
Pseudocode for Removing Nodes
 Nodes often need to be removed from a linked list.
 As with insertion, there is a technique for removing
a node from the front of a list, and a technique for
removing a node from elsewhere.
 We’ll look at the pseudocode for removing a node
from the front of a linked list.
Removing the Head Node
10 15 7
null
head_ptr
13
 Start by setting up a temporary pointer named remove_ptr to
the head node.
remove_ptr
Removing the Head Node
10 15 7
null
head_ptr
13
 Set up remove_ptr.
 head_ptr = remove_ptr->link( );
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
 Set up remove_ptr.
 head_ptr = remove_ptr->link( );
remove_ptr
Removing the Head Node
 Set up remove_ptr.
 head_ptr = remove_ptr->link( );
 delete remove_ptr; // Return the node's memory to heap.
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
 It is easy to insert a node at the front of a list.
 The linked list toolkit also provides a function for
inserting a new node elsewhere
 It is easy to remove a node at the front of a list.
 The linked list toolkit also provides a function for
removing a node elsewhere--you should read
about this function and the other functions of the
toolkit.
Summary
THE END

More Related Content

Similar to linkedlistwith animations.ppt

C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
BrianGHiNewmanv
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming Homework Help
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
Linked list
Linked listLinked list
Linked list
A. S. M. Shafi
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
ssuser0be977
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
Waf1231
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
JAGDEEPKUMAR23
 
Link List
Link ListLink List
Link List
umiekalsum
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
shashankbhadouria4
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
Kaynattariq1
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
vishalateen
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
muskans14
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 

Similar to linkedlistwith animations.ppt (20)

C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Linked list
Linked listLinked list
Linked list
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Link List
Link ListLink List
Link List
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
Linked list
Linked list Linked list
Linked list
 

Recently uploaded

Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 

Recently uploaded (20)

Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 

linkedlistwith animations.ppt

  • 1.  This presentation shows how to implement the most common operations on linked lists. Linked Lists in Action
  • 2.  For this presentation, nodes in a linked list are objects, as shown here. 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
  • 3.  The data_field of each node is a type called value_type, defined by a typedef. 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
  • 4.  Each node also contains a link_field which is a pointer to another node. 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
  • 5. Declarations for Linked Lists  A program can keep track of the front node by using a pointer variable such as head_ptr in this example.  Notice that head_ptr is not a node -- it is a pointer to a node. head_ptr data_field link_field 10 data_field link_field 15 data_field link_field 7 null
  • 6. Declarations for Linked Lists  A program can keep track of the front node by using a pointer variable such as head_ptr.  Notice that head_ptr is not a node -- it is a pointer to a node.  We represent the empty list by storing null in the head pointer. head_ptr null
  • 7. void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front We want to add a new entry, 13, to the front of the linked list shown here. 10 15 7 null head_ptr entry 13
  • 8. Inserting a Node at the Front  Create a new node, pointed to by a local variable insert_ptr. 10 15 7 null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 9. Inserting a Node at the Front insert_ptr = new node; 10 15 7 null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 10. Inserting a Node at the Front 10 15 7 null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 11. Inserting a Node at the Front 10 15 7 null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. Connect the new node to the front of the list. void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 12. Inserting a Node at the Front 10 15 7 null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); The correct new node can be completely created in one step by calling an appropriate node constructor. void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 13. Inserting a Node at the Front 10 15 7 null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); Make the old head pointer point to the new node. void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 14. Inserting a Node at the Front 10 15 7 null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; void list_head_insert(node*& head_ptr, const node::value_type& entry);
  • 15. Inserting a Node at the Front insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; 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);
  • 16. 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
  • 17. 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 ?
  • 18. head_ptr entry 13 null Inserting a Node at the Front 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; }
  • 19. 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
  • 20. 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; }
  • 21. 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.
  • 22. Caution!  Always make sure that your linked list functions work correctly with an empty list. EMPTY LIST
  • 23. Pseudocode for Inserting Nodes  Nodes are often inserted at places other than the front of a linked list.  There is a general pseudocode that you can follow for any insertion function. . .
  • 24. Pseudocode for Inserting Nodes  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);
  • 25. Pseudocode for Inserting Nodes  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);
  • 26. Pseudocode for Inserting Nodes  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); A pointer to the head of the list
  • 27. Pseudocode for Inserting Nodes  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);
  • 28. Pseudocode for Inserting Nodes  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.
  • 29. Pseudocode for Inserting Nodes 15 10 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. In this example, the new node will be the second node previous_ptr
  • 30. Pseudocode for Inserting Nodes 15 10 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 What is the name of this orange pointer ? Look at the pointer which is in the node *previous_ptr previous_ptr
  • 31. Pseudocode for Inserting Nodes 15 10 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 This pointer is called previous_ptr->link_field (although this name may be private to the node) What is the name of this orange pointer ? previous_ptr
  • 32. Pseudocode for Inserting Nodes 15 10 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 previous_ptr->link_field points to the head of a small linked list, with 10 and 7 previous_ptr
  • 33. Pseudocode for Inserting Nodes 15 10 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. The new node must be inserted at the front of this small linked list. 13 Write one C++ statement which will do the insertion. previous_ptr
  • 34. Pseudocode for Inserting Nodes 15 10 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 What might cause this statement to fail to compile? previous_ptr list_head_insert(previous_ptr->link_field, entry);
  • 35. Pseudocode for Inserting Nodes 15 10 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 Use a node member function to get the link field if needed. previous_ptr list_head_insert(previous_ptr->link( ), entry);
  • 36. Pseudocode for Inserting Nodes  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);  Otherwise (if the new node will not be first):  Set a pointer named previous_ptr to point to the node which is just before the new node's position.  Make the function call: list_head_insert(previous_ptr->link( ), entry);
  • 37. Pseudocode for Inserting Nodes  The process of adding a new node in the middle of a list can also be incorporated as a separate function. This function is called list_insert in the linked list toolkit of Section 5.2.
  • 38. Pseudocode for Removing Nodes  Nodes often need to be removed from a linked list.  As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere.  We’ll look at the pseudocode for removing a node from the front of a linked list.
  • 39. Removing the Head Node 10 15 7 null head_ptr 13  Start by setting up a temporary pointer named remove_ptr to the head node. remove_ptr
  • 40. Removing the Head Node 10 15 7 null head_ptr 13  Set up remove_ptr.  head_ptr = remove_ptr->link( ); remove_ptr Draw the change that this statement will make to the linked list.
  • 41. Removing the Head Node 10 15 7 null head_ptr 13  Set up remove_ptr.  head_ptr = remove_ptr->link( ); remove_ptr
  • 42. Removing the Head Node  Set up remove_ptr.  head_ptr = remove_ptr->link( );  delete remove_ptr; // Return the node's memory to heap. 10 15 7 null head_ptr 13 remove_ptr
  • 43. Removing the Head Node Here’s what the linked list looks like after the removal finishes. 10 15 7 null head_ptr
  • 44.  It is easy to insert a node at the front of a list.  The linked list toolkit also provides a function for inserting a new node elsewhere  It is easy to remove a node at the front of a list.  The linked list toolkit also provides a function for removing a node elsewhere--you should read about this function and the other functions of the toolkit. Summary