SlideShare a Scribd company logo
Linked Lists / Slide 1
Finding a node
 int FindNode(double x)
 Search for a node with the value equal to x in the list.
 If such a node is found, return its position. Otherwise, return
0.
int List::FindNode(double x) {
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode) return currIndex;
return 0;
}
Linked Lists / Slide 2
Deleting a node
 int DeleteNode(double x)
 Delete a node with the value equal to x from the list.
 If such a node is found, return its position. Otherwise, return
0.
 Steps
 Find the desirable node (similar to FindNode)
 Release the memory occupied by the found node
 Set the pointer of the predecessor of the found node to the
successor of the found node
 Like InsertNode, there are two special cases
 Delete first node
 Delete the node in middle or at the end of the list
Linked Lists / Slide 3
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
Try to find the node with
its value equal to x
Linked Lists / Slide 4
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
currNode
prevNode
Linked Lists / Slide 5
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
currNode
head
Linked Lists / Slide 6
Printing all the elements
 void DisplayList(void)
 Print the data of all the elements
 Print the number of the nodes in the list
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
Linked Lists / Slide 7
Destroying the list
 ~List(void)
 Use the destructor to release all the memory used by the list.
 Step through the list and delete each node one by one.
List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
delete currNode;
currNode = nextNode;
}
}
Linked Lists / Slide 8
Using List
int main(void)
{
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}
6
7
5
Number of nodes in the list: 3
5.0 found
4.5 not found
6
5
Number of nodes in the list: 2
result
Linked Lists / Slide 9
Variations of Linked Lists
 Circular linked lists
 The last node points to the first node of the list
 How do we know when we have finished traversing
the list? (Tip: check if the pointer of the current
node is equal to the head.)
A
Head
B C
Linked Lists / Slide 10
Variations of Linked Lists
 Doubly linked lists
 Each node points to not only successor but the
predecessor
 There are two NULL: at the first and last nodes in the
list
 Advantage: given a node, it is easy to visit its
predecessor. Convenient to traverse lists backwards
A
Head
B
 C 
Linked Lists / Slide 11
Array versus Linked Lists
 Linked lists are more complex to code and manage
than arrays, but they have some distinct advantages.
 Dynamic: a linked list can easily grow and shrink in size.
 We don’t need to know how many nodes will be in the list. They
are created in memory as needed.
 In contrast, the size of a C++ array is fixed at compilation time.
 Easy and fast insertions and deletions
 To insert or delete an element in an array, we need to copy to
temporary variables to make room for new elements or close the
gap caused by deleted elements.
 With a linked list, no need to move other nodes. Only need to
reset some pointers.

More Related Content

PPT
Abstract data types
PPT
linked-list - Abstract data type (ADT) Linked Lists
PPTX
Lecture 4 data structures and algorithms
PPT
List
PPT
linked-list.ppt
PPTX
3.linked list
PPT
Array linked list.ppt
PPT
17 linkedlist (1)
Abstract data types
linked-list - Abstract data type (ADT) Linked Lists
Lecture 4 data structures and algorithms
List
linked-list.ppt
3.linked list
Array linked list.ppt
17 linkedlist (1)

Similar to linked-list - Finding a node Deleting a node (20)

PPT
Linkedlist
PPTX
Linked list
PPT
Algo>ADT list & linked list
PPTX
C Exam Help
PPTX
Implemention of Linked list concept in Data Structures
PPTX
Data Structure and Algorithm Lesson 2.pptx
PPT
Linked list1.ppt
PPTX
Linked lists a
PDF
LinkedList1LinkedList1LinkedList1111.pdf
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PPTX
Linked list
PPTX
C Homework Help
PPT
Fundamentals of data structures
PPTX
linkedlistforslideshare-210123143943.pptx
PDF
Unit - 2.pdf
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
PDF
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
PDF
How to delete one specific node in linked list in CThanksSolu.pdf
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
Linkedlist
Linked list
Algo>ADT list & linked list
C Exam Help
Implemention of Linked list concept in Data Structures
Data Structure and Algorithm Lesson 2.pptx
Linked list1.ppt
Linked lists a
LinkedList1LinkedList1LinkedList1111.pdf
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Linked list
C Homework Help
Fundamentals of data structures
linkedlistforslideshare-210123143943.pptx
Unit - 2.pdf
Data Structures in C++I am really new to C++, so links are really .pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
How to delete one specific node in linked list in CThanksSolu.pdf
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
Ad

More from Anil Yadav (20)

PPTX
Link List : Introduction to List and Linked Lists
PPTX
Link List REPRESENTATION OF DOUBLY LINKED LIST
PPTX
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
PPTX
Link List STACK and Queue USING LINKED LIST
PPTX
Link List Programming Linked List in Cpp
PPTX
Link List & ALGORITHM FOR DELETING A NODE
PPTX
Link List ALGORITHM FOR INSERTING A NODE
PPTX
Presentations Linked Lists Data Structure
PPT
Lec-12, 13 Quees First In First Out (FIFO)
PPT
Lec-12, 13 Quee s Applications of Queues
PPT
Lec-12, 13 Quees Array Implementation IN
PPT
Lec-12, 13 Quees In Queue IntQueue(int s)
PPT
Lec-12, 13 Quees A class for Dynamic Queue implementation
PPT
Function enqueue inserts the value in num
PPT
Lec-12, 13 Quees -How to determine empty and full Queues?
PDF
Unit2-BIS Business Information system Data
PPT
Lec-12, 13 Queues - IntQueue IntQueue(int s) //constructor
PPT
Lec-12, 13 Quees Another implementation of Queues using Arrays
PPT
Lec-12, 13 Quees - Circular Queues and Implementation with Array
PPT
Lec-32 Recursion - Divide and Conquer in Queue
Link List : Introduction to List and Linked Lists
Link List REPRESENTATION OF DOUBLY LINKED LIST
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
Link List STACK and Queue USING LINKED LIST
Link List Programming Linked List in Cpp
Link List & ALGORITHM FOR DELETING A NODE
Link List ALGORITHM FOR INSERTING A NODE
Presentations Linked Lists Data Structure
Lec-12, 13 Quees First In First Out (FIFO)
Lec-12, 13 Quee s Applications of Queues
Lec-12, 13 Quees Array Implementation IN
Lec-12, 13 Quees In Queue IntQueue(int s)
Lec-12, 13 Quees A class for Dynamic Queue implementation
Function enqueue inserts the value in num
Lec-12, 13 Quees -How to determine empty and full Queues?
Unit2-BIS Business Information system Data
Lec-12, 13 Queues - IntQueue IntQueue(int s) //constructor
Lec-12, 13 Quees Another implementation of Queues using Arrays
Lec-12, 13 Quees - Circular Queues and Implementation with Array
Lec-32 Recursion - Divide and Conquer in Queue
Ad

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Basic Mud Logging Guide for educational purpose
PDF
RMMM.pdf make it easy to upload and study
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
TR - Agricultural Crops Production NC III.pdf
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Anesthesia in Laparoscopic Surgery in India
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Basic Mud Logging Guide for educational purpose
RMMM.pdf make it easy to upload and study
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Types and Its function , kingdom of life
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
STATICS OF THE RIGID BODIES Hibbelers.pdf

linked-list - Finding a node Deleting a node

  • 1. Linked Lists / Slide 1 Finding a node  int FindNode(double x)  Search for a node with the value equal to x in the list.  If such a node is found, return its position. Otherwise, return 0. int List::FindNode(double x) { Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { currNode = currNode->next; currIndex++; } if (currNode) return currIndex; return 0; }
  • 2. Linked Lists / Slide 2 Deleting a node  int DeleteNode(double x)  Delete a node with the value equal to x from the list.  If such a node is found, return its position. Otherwise, return 0.  Steps  Find the desirable node (similar to FindNode)  Release the memory occupied by the found node  Set the pointer of the predecessor of the found node to the successor of the found node  Like InsertNode, there are two special cases  Delete first node  Delete the node in middle or at the end of the list
  • 3. Linked Lists / Slide 3 Deleting a node int List::DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } Try to find the node with its value equal to x
  • 4. Linked Lists / Slide 4 Deleting a node int List::DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } currNode prevNode
  • 5. Linked Lists / Slide 5 Deleting a node int List::DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; } currNode head
  • 6. Linked Lists / Slide 6 Printing all the elements  void DisplayList(void)  Print the data of all the elements  Print the number of the nodes in the list void List::DisplayList() { int num = 0; Node* currNode = head; while (currNode != NULL){ cout << currNode->data << endl; currNode = currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl; }
  • 7. Linked Lists / Slide 7 Destroying the list  ~List(void)  Use the destructor to release all the memory used by the list.  Step through the list and delete each node one by one. List::~List(void) { Node* currNode = head, *nextNode = NULL; while (currNode != NULL) { nextNode = currNode->next; // destroy the current node delete currNode; currNode = nextNode; } }
  • 8. Linked Lists / Slide 8 Using List int main(void) { List list; list.InsertNode(0, 7.0); // successful list.InsertNode(1, 5.0); // successful list.InsertNode(-1, 5.0); // unsuccessful list.InsertNode(0, 6.0); // successful list.InsertNode(8, 4.0); // unsuccessful // print all the elements list.DisplayList(); if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl; else cout << "5.0 not found" << endl; if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl; else cout << "4.5 not found" << endl; list.DeleteNode(7.0); list.DisplayList(); return 0; } 6 7 5 Number of nodes in the list: 3 5.0 found 4.5 not found 6 5 Number of nodes in the list: 2 result
  • 9. Linked Lists / Slide 9 Variations of Linked Lists  Circular linked lists  The last node points to the first node of the list  How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A Head B C
  • 10. Linked Lists / Slide 10 Variations of Linked Lists  Doubly linked lists  Each node points to not only successor but the predecessor  There are two NULL: at the first and last nodes in the list  Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A Head B  C 
  • 11. Linked Lists / Slide 11 Array versus Linked Lists  Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.  Dynamic: a linked list can easily grow and shrink in size.  We don’t need to know how many nodes will be in the list. They are created in memory as needed.  In contrast, the size of a C++ array is fixed at compilation time.  Easy and fast insertions and deletions  To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.  With a linked list, no need to move other nodes. Only need to reset some pointers.