SlideShare a Scribd company logo
1 of 30
Linked List
 More on Pointers and Linked Lists
Review of Sequential
Representations
 Previously introduced data structures, including
array, queue, and stack, they all have the property
that successive nodes of data object were stored a
fixed distance apart.
 The drawback of sequential mapping for ordered lists
is that operations such as insertion and deletion
become expensive.
 Also sequential representation tends to have less
space efficiency when handling multiple various sizes
of ordered lists.
Linked List
 A better solutions to resolve the aforementioned
issues of sequential representations is linked lists.
 Elements in a linked list are not stored in sequential
in memory. Instead, they are stored all over the
memory. They form a list by recording the address of
next element for each element in the list. Therefore,
the list is linked together.
 A linked list has a head pointer that points to the first
element of the list.
 By following the links, you can traverse the linked list
and visit each element in the list one by one.
Nodes and Linked Lists
 A linked list is a list that can grow and shrink while
the program is running
 A linked list is constructed using pointers
 A linked list often consists of classes that contain a
pointer variable connecting them to other dynamic
variables.
 A linked list can be visualized as items, drawn as
boxes, connected to other items by arrows
1
0
15.1
12 14 en
d
first
Nodes
 The boxes in the previous drawing represent
the nodes of a linked list
 Nodes contain the data item(s) and a pointer that
can point to another node of the same type

The pointers point to the entire node, not an individual
item that might be in the node
 The arrows in the drawing represent pointers
The head of a List
 The box labeled first, in figure , is not a
node, but a pointer variable that points
to a node
 Pointer variable first is declared as:
Node *first;
NULL
 The defined constant NULL is used as…
 An end marker for a linked list

A program can step through a list of nodes by following
the pointers, but when it finds a node containing NULL,
it knows it has come to the end of the list
 The value of a pointer that has nothing to point to
 The value of NULL is 0
 Any pointer can be assigned the value NULL:
int *ptr = NULL;
Designing a List in C++
 Use of two classes. Create a class that represents
the linked list. The class contains the items of another
objects of another class.
Linked Lists
 A linked list is a list of nodes in which each
node has one or more member variable and a
pointer that “points” to the next node in the
list
 The first node is called the head
 The pointer variable first, points to the first node

The pointer named first is not the head of the list…it
points to the head of the list
 The last node contains a pointer set to NULL
Friend Classes
class Chain; // forward declaration
Class Node {
friend class Chain;
private:
int info;
Node * link;
};
Class Chain {
public:
// Chain Manipulation operations
· · ·
private:
Node *first;
};
Function head_insert
 It would be better to create a function to insert
nodes at the head of a list, such as:
 void head_insert(int val);

val is the value to be stored in the list
 head_insert will create a new node for the val

The val will be copied to the new node

The new node will be inserted in the list as the new
head node
Pseudocode for head_insert
 Create a new dynamic variable pointed to by
temp.
 Place the value in data part of the new node
 Make temp's link variable point to the head
node
 Make the first pointer point to temp.
Translating head_insert to C++
 The pseudocode for head_insert can be
written as
pseudocode:
 Node *temp; //create the temporary pointer
temp = new Node; // create the new node
 temp->info = number; //copy the number
 temp->link = first; //new node points to first node
first = temp; // head points to new
// first node
An Empty List
 A list with nothing in it is called an empty list
 The first pointer of an empty list is NULL
first = NULL;
 Any functions written to manipulate a linked list
should check to see if it works on the empty list
 Initialize first with NULL in constructor of
Chain class.
Traversing a Linked List
 To design a function that will traverse
particular node in a linked list:
 We want the function to display all data in the link
list so we can need to use a temporary pointer that
can be moved forward.
 First pointer should not be used to traverse the link
list as when it is moved forward the result will be
memory leaks.
 This declaration will work:
void display();
Pseudocode for traversal
1. Make pointer variable temp point to the
head node
2. while(temp is not null)
{
display data in the node
make temp point to the next node
}
Moving Through the List
 The pseudocode for search requires the
pointer temp to move through the list
 How does temp follow the pointers from node to
node?
 When temp points to a node, temp->link is the
address of the next node
 To make temp point to the next node, make the
assignment:
temp = temp->link;
Translating display to C++
 The pseudocode for display can be written in
C++ using these lines in place of the lines of
pseudocode:
 Node *temp; //create the temporary pointer
temp = first; // initialize it with head node
 while (temp!= NULL)
{
cout << temp->info;
temp = temp->link // temp points to next node }
Searching a Linked List
 To design a function that will locate a
particular
node in a linked list:
 We want the function to return a pointer to the
node so we can use the data if we find it, else
return NULL
 The data we wish to find is the argument
 This declaration will work:
Node* search(int key);
Pseudocode for search
1. Make pointer variable temp point to the head
node
2. While(temp does not point to a node
containing key AND temp does not point to
the last node)
{
make temp point to the next node
}
3. If (temp points to a node containing the key)
return temp;
else
return NULL;
A Refinement of search
 The search function can be refined in this way:
temp = first;
while(temp->info!= key && temp->link != NULL)
{
temp = temp->link;
}
if (temp->info = = key)
return temp;
else
return NULL;
Check for last node
Searching an Empty List
 Our search algorithm has a problem
 If the list is empty, here equals NULL before the
while loop so…

temp->data is undefined

temp->link is undefined
 So do search if first is not equal to NULL.
Deleting a Node From Head
 To delete a node from a linked list
 Store the address of node to be deleted in a
temporary pointer temp.
 Assign the address of the second node to first
pointer
 The node is removed from the list, but is still in
memory
 Return *temp to the freestore: delete temp;
Pseudocode for Deletion
1. Make pointer variable temp point to the
head node
2. Make head point to the second node
3. De-allocate memory for the first node.
Translating head_del to C++
 The pseudocode for head_del can be written
in C++ using these lines in place of the lines
of
pseudocode:
 Node *temp; //create the temporary pointer
temp = first;
 first = first ->link;
 delete temp;
Translating head_del to C++
 The pseudocode for head_del can be written
in C++ using these lines in place of the lines
of
pseudocode:
 Node *temp; //create the temporary pointer
temp = first;
 first = first ->link;
 delete temp;
1
0
12 14 en
d
first
Temp
Insert at end
Inserts an element at the end of the list
void insertend(int v)
{ Node *temp = first;
while (temp -> link != NULL)
temp = temp->link;
Node *newer = new Node;
newer->info = v;
newer->link = NULL;
temp->link = newer;
}
1
0
12 14 ×head
newer
16 ×
temp
Delete a Node after a Node
Delete an element after the specific node
Node *t = Search(key); delafter(t);
void delafter(Node *temp)
{ Node *temp1 = temp->link;
int x = temp->data;
temp = temp->link;
delete temp;
}
1
0
12 14 ×first 16 ×
temp temp1
Header Node
 Header node can contain global nodes that
can contain global information about a list like
 Length of the list.
 Pointer to the last node.
Like
struct charstr
{
int length;
Node *first;
}s,s1;
Assignment
• Merge two lists.
• Search a number from list and delete it.
• Delete all occurrences of a number from the list.

More Related Content

What's hot

Circular linked list
Circular linked listCircular linked list
Circular linked list
dchuynh
 

What's hot (20)

Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Link List
Link ListLink List
Link List
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Linked list
Linked listLinked list
Linked list
 
Ch17
Ch17Ch17
Ch17
 
Link list
Link listLink list
Link list
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Linked list
Linked listLinked list
Linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Doc 20180130-wa0003
Doc 20180130-wa0003Doc 20180130-wa0003
Doc 20180130-wa0003
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linked list
Linked listLinked list
Linked list
 
Savitch ch 13
Savitch ch 13Savitch ch 13
Savitch ch 13
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Linked list
Linked listLinked list
Linked list
 

Similar to Link list part 1

Data structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTData structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LIST
binakasehun2026
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
Amit Vats
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 

Similar to Link list part 1 (20)

Data structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LISTData structure and algorithms chapter three LINKED LIST
Data structure and algorithms chapter three LINKED LIST
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Linked List
Linked ListLinked List
Linked List
 
Linked lists
Linked listsLinked lists
Linked lists
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Savitch Ch 13
Savitch Ch 13Savitch Ch 13
Savitch Ch 13
 
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...
 
Cpp lernaufgabe linked_list
Cpp lernaufgabe linked_listCpp lernaufgabe linked_list
Cpp lernaufgabe linked_list
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Linked list
Linked listLinked list
Linked list
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Linked list
Linked listLinked list
Linked list
 

More from Anaya Zafar

More from Anaya Zafar (20)

data structures and its importance
 data structures and its importance  data structures and its importance
data structures and its importance
 
heap sort
 heap sort heap sort
heap sort
 
Lec 2 algorithms efficiency complexity
Lec 2 algorithms efficiency  complexityLec 2 algorithms efficiency  complexity
Lec 2 algorithms efficiency complexity
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
Dijkstra's algorithm
Dijkstra's algorithm Dijkstra's algorithm
Dijkstra's algorithm
 
The miracle-morning
The miracle-morningThe miracle-morning
The miracle-morning
 
How to write a meeting agenda?
How to write a  meeting agenda?How to write a  meeting agenda?
How to write a meeting agenda?
 
How we achieve our goals
How we achieve our goalsHow we achieve our goals
How we achieve our goals
 
Fiber optics
Fiber opticsFiber optics
Fiber optics
 
3 d technology
3 d technology3 d technology
3 d technology
 
Ch 22 question solution of fundamental of physics 8th edition by HRW
Ch 22  question solution of fundamental of physics 8th edition by HRWCh 22  question solution of fundamental of physics 8th edition by HRW
Ch 22 question solution of fundamental of physics 8th edition by HRW
 
Ch 21 question solution of fundamental of physics 8th edition by HRW
Ch 21 question solution of fundamental of physics 8th edition by HRWCh 21 question solution of fundamental of physics 8th edition by HRW
Ch 21 question solution of fundamental of physics 8th edition by HRW
 
Definition of capacitance
Definition of capacitanceDefinition of capacitance
Definition of capacitance
 
Chapter 24-capacitance
Chapter 24-capacitanceChapter 24-capacitance
Chapter 24-capacitance
 
Capacitance and dielectrics
Capacitance and dielectrics Capacitance and dielectrics
Capacitance and dielectrics
 
20 electric current resistance ohms law
20 electric current resistance ohms law20 electric current resistance ohms law
20 electric current resistance ohms law
 
Voltage, current, resistance, and ohm's law
Voltage, current, resistance, and ohm's lawVoltage, current, resistance, and ohm's law
Voltage, current, resistance, and ohm's law
 
Role of women in development
Role of women in developmentRole of women in development
Role of women in development
 
Application of Gauss's law
Application of  Gauss's lawApplication of  Gauss's law
Application of Gauss's law
 
Why we need Gaussian surface in Gauss's law
Why we need Gaussian surface in Gauss's lawWhy we need Gaussian surface in Gauss's law
Why we need Gaussian surface in Gauss's law
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Link list part 1

  • 1. Linked List  More on Pointers and Linked Lists
  • 2. Review of Sequential Representations  Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart.  The drawback of sequential mapping for ordered lists is that operations such as insertion and deletion become expensive.  Also sequential representation tends to have less space efficiency when handling multiple various sizes of ordered lists.
  • 3. Linked List  A better solutions to resolve the aforementioned issues of sequential representations is linked lists.  Elements in a linked list are not stored in sequential in memory. Instead, they are stored all over the memory. They form a list by recording the address of next element for each element in the list. Therefore, the list is linked together.  A linked list has a head pointer that points to the first element of the list.  By following the links, you can traverse the linked list and visit each element in the list one by one.
  • 4. Nodes and Linked Lists  A linked list is a list that can grow and shrink while the program is running  A linked list is constructed using pointers  A linked list often consists of classes that contain a pointer variable connecting them to other dynamic variables.  A linked list can be visualized as items, drawn as boxes, connected to other items by arrows 1 0 15.1 12 14 en d first
  • 5. Nodes  The boxes in the previous drawing represent the nodes of a linked list  Nodes contain the data item(s) and a pointer that can point to another node of the same type  The pointers point to the entire node, not an individual item that might be in the node  The arrows in the drawing represent pointers
  • 6. The head of a List  The box labeled first, in figure , is not a node, but a pointer variable that points to a node  Pointer variable first is declared as: Node *first;
  • 7. NULL  The defined constant NULL is used as…  An end marker for a linked list  A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list  The value of a pointer that has nothing to point to  The value of NULL is 0  Any pointer can be assigned the value NULL: int *ptr = NULL;
  • 8. Designing a List in C++  Use of two classes. Create a class that represents the linked list. The class contains the items of another objects of another class.
  • 9. Linked Lists  A linked list is a list of nodes in which each node has one or more member variable and a pointer that “points” to the next node in the list  The first node is called the head  The pointer variable first, points to the first node  The pointer named first is not the head of the list…it points to the head of the list  The last node contains a pointer set to NULL
  • 10. Friend Classes class Chain; // forward declaration Class Node { friend class Chain; private: int info; Node * link; }; Class Chain { public: // Chain Manipulation operations · · · private: Node *first; };
  • 11. Function head_insert  It would be better to create a function to insert nodes at the head of a list, such as:  void head_insert(int val);  val is the value to be stored in the list  head_insert will create a new node for the val  The val will be copied to the new node  The new node will be inserted in the list as the new head node
  • 12. Pseudocode for head_insert  Create a new dynamic variable pointed to by temp.  Place the value in data part of the new node  Make temp's link variable point to the head node  Make the first pointer point to temp.
  • 13. Translating head_insert to C++  The pseudocode for head_insert can be written as pseudocode:  Node *temp; //create the temporary pointer temp = new Node; // create the new node  temp->info = number; //copy the number  temp->link = first; //new node points to first node first = temp; // head points to new // first node
  • 14. An Empty List  A list with nothing in it is called an empty list  The first pointer of an empty list is NULL first = NULL;  Any functions written to manipulate a linked list should check to see if it works on the empty list  Initialize first with NULL in constructor of Chain class.
  • 15. Traversing a Linked List  To design a function that will traverse particular node in a linked list:  We want the function to display all data in the link list so we can need to use a temporary pointer that can be moved forward.  First pointer should not be used to traverse the link list as when it is moved forward the result will be memory leaks.  This declaration will work: void display();
  • 16. Pseudocode for traversal 1. Make pointer variable temp point to the head node 2. while(temp is not null) { display data in the node make temp point to the next node }
  • 17. Moving Through the List  The pseudocode for search requires the pointer temp to move through the list  How does temp follow the pointers from node to node?  When temp points to a node, temp->link is the address of the next node  To make temp point to the next node, make the assignment: temp = temp->link;
  • 18. Translating display to C++  The pseudocode for display can be written in C++ using these lines in place of the lines of pseudocode:  Node *temp; //create the temporary pointer temp = first; // initialize it with head node  while (temp!= NULL) { cout << temp->info; temp = temp->link // temp points to next node }
  • 19. Searching a Linked List  To design a function that will locate a particular node in a linked list:  We want the function to return a pointer to the node so we can use the data if we find it, else return NULL  The data we wish to find is the argument  This declaration will work: Node* search(int key);
  • 20. Pseudocode for search 1. Make pointer variable temp point to the head node 2. While(temp does not point to a node containing key AND temp does not point to the last node) { make temp point to the next node } 3. If (temp points to a node containing the key) return temp; else return NULL;
  • 21. A Refinement of search  The search function can be refined in this way: temp = first; while(temp->info!= key && temp->link != NULL) { temp = temp->link; } if (temp->info = = key) return temp; else return NULL; Check for last node
  • 22. Searching an Empty List  Our search algorithm has a problem  If the list is empty, here equals NULL before the while loop so…  temp->data is undefined  temp->link is undefined  So do search if first is not equal to NULL.
  • 23. Deleting a Node From Head  To delete a node from a linked list  Store the address of node to be deleted in a temporary pointer temp.  Assign the address of the second node to first pointer  The node is removed from the list, but is still in memory  Return *temp to the freestore: delete temp;
  • 24. Pseudocode for Deletion 1. Make pointer variable temp point to the head node 2. Make head point to the second node 3. De-allocate memory for the first node.
  • 25. Translating head_del to C++  The pseudocode for head_del can be written in C++ using these lines in place of the lines of pseudocode:  Node *temp; //create the temporary pointer temp = first;  first = first ->link;  delete temp;
  • 26. Translating head_del to C++  The pseudocode for head_del can be written in C++ using these lines in place of the lines of pseudocode:  Node *temp; //create the temporary pointer temp = first;  first = first ->link;  delete temp; 1 0 12 14 en d first Temp
  • 27. Insert at end Inserts an element at the end of the list void insertend(int v) { Node *temp = first; while (temp -> link != NULL) temp = temp->link; Node *newer = new Node; newer->info = v; newer->link = NULL; temp->link = newer; } 1 0 12 14 ×head newer 16 × temp
  • 28. Delete a Node after a Node Delete an element after the specific node Node *t = Search(key); delafter(t); void delafter(Node *temp) { Node *temp1 = temp->link; int x = temp->data; temp = temp->link; delete temp; } 1 0 12 14 ×first 16 × temp temp1
  • 29. Header Node  Header node can contain global nodes that can contain global information about a list like  Length of the list.  Pointer to the last node. Like struct charstr { int length; Node *first; }s,s1;
  • 30. Assignment • Merge two lists. • Search a number from list and delete it. • Delete all occurrences of a number from the list.