SlideShare a Scribd company logo
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

PPTX
Data Structures - Lecture 7 [Linked List]
PPTX
Linked lists in Data Structure
PPTX
Linked lists 1
PPTX
PPT
linked list
PPT
Singly link list
PPT
Operations on linked list
PPTX
Linked list
Data Structures - Lecture 7 [Linked List]
Linked lists in Data Structure
Linked lists 1
linked list
Singly link list
Operations on linked list
Linked list

What's hot (20)

PPTX
Dounly linked list
PPT
Link List
PPT
Circular linked list
PPT
Linked list
PPT
Ch17
PPT
Link list
PDF
Linked List Static and Dynamic Memory Allocation
PPT
Linked list
PPT
Unit ii(dsc++)
PDF
Doc 20180130-wa0003
PPS
Single linked list
PPT
Algo>ADT list & linked list
PPTX
Linked list
PPT
Savitch ch 13
PPTX
Link list presentation slide(Daffodil international university)
PPT
Data Structure and Algorithms Linked List
PPT
Data Structure Lecture 6
PDF
Circular linked list
PDF
Singly linked list
PPTX
Linked list
Dounly linked list
Link List
Circular linked list
Linked list
Ch17
Link list
Linked List Static and Dynamic Memory Allocation
Linked list
Unit ii(dsc++)
Doc 20180130-wa0003
Single linked list
Algo>ADT list & linked list
Linked list
Savitch ch 13
Link list presentation slide(Daffodil international university)
Data Structure and Algorithms Linked List
Data Structure Lecture 6
Circular linked list
Singly linked list
Linked list
Ad

Similar to Link list part 1 (20)

PPT
linked_lists.ppt linked_lists linked_lists
PPTX
singlelinkedlistasdfghzxcvbnmqwertyuiopa
PPT
Savitch Ch 13
PPT
linkedlistwith animations.ppt
PPT
Abstract data types
PPT
linked-list - Abstract data type (ADT) Linked Lists
PPT
Lecture 3 List of Data Structures & Algorithms
PDF
Cpp lernaufgabe linked_list
PPTX
DS_LinkedList.pptx
PPTX
LEC_4,5_linked_list.pptx this is Good for data structure
PPTX
LEC_4,5_linked_list.pptx for single and double linked list
PPTX
Linked lists a
PDF
Linked Lists.pdf
PPT
Chapter 5 ds
PPTX
link listgyyfghhchgfvgggfshiskabaji.pptx
PPT
dynamicList.ppt
PPTX
Lec3-Linked list.pptx
PPT
Mi 103 linked list
PPTX
Data structure and algorithm list structures
PPTX
csc211_lecture_21.pptx
linked_lists.ppt linked_lists linked_lists
singlelinkedlistasdfghzxcvbnmqwertyuiopa
Savitch Ch 13
linkedlistwith animations.ppt
Abstract data types
linked-list - Abstract data type (ADT) Linked Lists
Lecture 3 List of Data Structures & Algorithms
Cpp lernaufgabe linked_list
DS_LinkedList.pptx
LEC_4,5_linked_list.pptx this is Good for data structure
LEC_4,5_linked_list.pptx for single and double linked list
Linked lists a
Linked Lists.pdf
Chapter 5 ds
link listgyyfghhchgfvgggfshiskabaji.pptx
dynamicList.ppt
Lec3-Linked list.pptx
Mi 103 linked list
Data structure and algorithm list structures
csc211_lecture_21.pptx
Ad

More from Anaya Zafar (20)

PPTX
data structures and its importance
PPTX
heap sort
PPTX
Lec 2 algorithms efficiency complexity
PPT
Link list part 2
PPTX
Dijkstra's algorithm
PDF
The miracle-morning
DOCX
How to write a meeting agenda?
PPTX
How we achieve our goals
DOCX
Fiber optics
PPTX
3 d technology
PDF
Ch 22 question solution of fundamental of physics 8th edition by HRW
PDF
Ch 21 question solution of fundamental of physics 8th edition by HRW
PDF
Definition of capacitance
PDF
Chapter 24-capacitance
PDF
Capacitance and dielectrics
PDF
20 electric current resistance ohms law
PDF
Voltage, current, resistance, and ohm's law
DOC
Role of women in development
PDF
Application of Gauss's law
PDF
Why we need Gaussian surface in Gauss's law
data structures and its importance
heap sort
Lec 2 algorithms efficiency complexity
Link list part 2
Dijkstra's algorithm
The miracle-morning
How to write a meeting agenda?
How we achieve our goals
Fiber optics
3 d technology
Ch 22 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
Chapter 24-capacitance
Capacitance and dielectrics
20 electric current resistance ohms law
Voltage, current, resistance, and ohm's law
Role of women in development
Application of Gauss's law
Why we need Gaussian surface in Gauss's law

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Pharma ospi slides which help in ospi learning
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Pharma ospi slides which help in ospi learning
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Microbial disease of the cardiovascular and lymphatic systems
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
A systematic review of self-coping strategies used by university students to ...
O7-L3 Supply Chain Operations - ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

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.