
Linked List
Data Structures & Algorithms
Syed Mudassar Alam
Linked Lists
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
A linked list is a series of connected nodes (or links)
where each node is a data structure.
Dynamically allocated data structures can be linked
together to form a chain.
A linked list can grow or shrink in size as the program
runs.This is possible because the nodes in a linked list are
dynamically allocated.
Defination:
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Lists and arrays
Lists:
Lists and arrays
Arrays:
{Size of the following array is = 4}
Index 0 1 2 3
Value 44 5 96 3
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Nodes and pointers
 Each nodes contains the Data
Field and a reference field (Pointers)
that points to next node.
 The last nodes contains the null value in its second
part because there is no node after it.
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Single linked lists
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Double Linked Lists
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Circular Lists
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Advantages
 Linked lists are more complex to code and manage
than arrays, but they have some distinct advantages.
a)A linked list can easily grow and shrink in size -
◦ The programmer doesn’t need to know how
many nodes will be in the list. They are created in
memory as needed.
b)Speed of insertion or deletion from the list -
◦ Inserting and deleting elements into and out of
arrays requires moving elements of the array. When
a node is inserted, or deleted from a linked list,
none of the other nodes have to be moved.
Basic Operations of a Linked List
 As an abstract data type,
A list is a finite sequence (possibly empty) of elements with basic
operations that vary from one application to another.
Basic operations commonly include:
 Construction: Allocate and initialize a list object (usually empty)
 Empty: Check if list is empty
Basic Operations of a Linked
List
 Traverse: Visit each element of the list in order stored
 Insert: Add an item to the list (at any point)
 Delete: Remove an item from the list (at any point)
Basic Operations of a Linked List
 Construction: first = null_value;
 Empty: return (first == null_value)
 Traverse:
ptr = first;
while (ptr != null_value)
{ Process data part of node pointed to by ptr;
ptr = address of next element
// contained in next part of
// node pointed to by ptr;
}
Basic Operations of a Linked
List
 Insert & Delete: Both operations necessitate the
changing of links.
The address of the predecessor of the node to be
added or removed needs to be known.
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle of the list
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle (any) of the list
Insertion at the top
Steps:
 Create a Node
 Set the node data Values
 Connect the pointers
Insertion Description
 Follow the previous steps and we get
48 17 142
head //
head 93
Step 1 Step 2
Step 3
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle of the list
Insertion at the end
Steps:
 Create a Node
 Set the node data Values
 Connect the pointers
Insertion Description
 Follow the previous steps and we get
48 17 142
head //
Step 1 Step 2
Step 3
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle of the list
Insertion in the middle
Steps:
 Create a Node
 Set the node data Values
 Break pointer connection
 Re-connect the pointers
Insertion Description
Step 1 Step 2
Step 3
Step 4
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deleting from the top
Steps
 Break the pointer connection
 Re-connect the nodes
 Delete the node
Deletion Description
4 17
head
42
6
4 17
head
42
6
4 17
head
42
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deleting from the end
Steps
 Break the pointer connection
 Set previous node pointer to NULL
 Delete the node
Deletion Description
4 17
head
42
6
4 17
head
42
6
4 17
6
head
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deleting from the Middle
Steps
 Set previous Node pointer to next node
 Break Node pointer connection
 Delete the node
Deletion Description
4 17 42
head
4 17
head
42
4
head
42
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Basic Node Implementation
The following code is written in C++:
Struct Node
{
int data; //any type of data could be another struct
Node *next; //this is an important piece of code
“pointer”
};
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Adding a Node
There are four steps to add a node to a linked list:
 Allocate memory for the new node.
 Determine the insertion point (you need to know only the
new node’s predecessor (pPre)
 Point the new node to its successor.
 Point the predecessor to the new node.
Pointer to the predecessor (pPre) can be in one of two
states:
 it can contain the address of a node (i.e. you are
adding somewhere after the first node – in the
middle or at the end)
 it can be NULL (i.e. you are adding either to an
empty list or at the beginning of the list)
Adding Nodes to an Empty Linked List
pNew -> next = pHead; // set link to NULL
pHead = pNew;// point list to first node
Before: After:
Page 51
51
39
pNew
pHead
pPre
39
pNew
pHead
pPre
Adding a Node to the Beginning of a
Linked List
39
pNew
pHead
pPre
75 124
39
pNew
pHead
pPre
75 124
pNew -> next = pHead;
pHead = pNew;// point list to first node
Adding a Node to the End of a Linked List
Before:
pNew -> next = NULL;
pPre ->next = pNew;
After:
144
pNew
pPre
55 24
144
pNew
pPre
55 24
Adding a Node to the Middle of a Linked List
64
pNew
pPre
55 124
Before:
pNew -> next = pPre -> next;
pPre -> next = pNew;
After: 64
pNew
55 124
pPre
Deleting a Node from a
Linked List
 Deleting a node requires that we logically remove the
node from the list by changing various links and then
physically deleting the node from the list (i.e., return it
to the heap).
 Any node in the list can be deleted. Note that if the
only node in the list is to be deleted, an empty list will
result. In this case the head pointer will be set to NULL.
 To logically delete a node:
 First locate the node itself (pCur) and its logical
predecessor (pPre).
 Change the predecessor’s link field to point to the
deleted node’s successor (located at pCur ->
next).
 Recycle the node
Deleting the First Node from a Linked List
Before:
pHead = pCur -> next;
delete(pCur);
After:
Page 57
pHead
pPre
75 124
pCur
pHead
pPre
Recycle
d
124
pCur
Deleting a Node from a Linked List
– General Case
 Before:
 After:
75 124
96
pPre pCur
75 124
Recycle
d
pPre pCur
Code:
pPre -> next = pCur -> next;
delete(pCur);
Doubly Link List
 It contains the data value
it contains two pointer
 Next, Prev
Insert at Rear End (Last)
Insert at Rear
Insert at rear
newnode->prev= tail
tail->next=newnode
tail=Newnode
List is Empty
Newnode->prev=tail //(if tail=null)
head=newnode
tail=newnode
Newnode->next=null
Insert at First
Newnode->next=head
head->prev=newnode
head=newnode
Insert at middle (some point)
 newnode->prev=current
 newnode->next=current->next
 current->next->prev=newnode
 current->next=newnode
Delete at rear end (last)
lNode* delNode=tail
tail=delNode->prev
tail->next=null
delete delnode
Delete only One node
 lNode* delNode=tail
 tail=delNode->prev
 head=null // (tail==null)
 delete delNode
Delete from Front
 lNode* delNode=head
 head= delnode->next
 head->prev=null
 delete delnode
Delete From Middle (Any)
 curr->prev->next=curr->next
 curr->next-prev=curr->prev
 delete curr
Reading Material CS
 Schaum’s Outlines: Chapter # 5
 D. S. Malik: Chapter # 5
 Mark A. Weiss: Chapter # 3
 Chapter 6, ADT, Data Structure and Problem solving
with C++ by Larry Nyhoff.
 Chapter 5, Nell Dale 3rd Edition
 Chapter 8, C++ an introduction to Data Structures by
Larry Nyhoff

Linked Lists.pdf

  • 1.
     Linked List Data Structures& Algorithms Syed Mudassar Alam
  • 2.
  • 3.
    Outline  Introduction  InsertionDescription  Deletion Description  Basic Node Implementation  Conclusion
  • 4.
    Outline  Introduction  InsertionDescription  Deletion Description  Basic Node Implementation  Conclusion
  • 5.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 6.
    A linked listis a series of connected nodes (or links) where each node is a data structure. Dynamically allocated data structures can be linked together to form a chain. A linked list can grow or shrink in size as the program runs.This is possible because the nodes in a linked list are dynamically allocated. Defination:
  • 7.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 8.
  • 9.
    Lists and arrays Arrays: {Sizeof the following array is = 4} Index 0 1 2 3 Value 44 5 96 3
  • 10.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 11.
    Nodes and pointers Each nodes contains the Data Field and a reference field (Pointers) that points to next node.  The last nodes contains the null value in its second part because there is no node after it.
  • 12.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 13.
  • 14.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 15.
  • 16.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 17.
  • 18.
    Introduction  Definitions  Listsand arrays  Nodes and pointers  Single Linked Lists  Double Linked Lists  Circular Lists  Advantages
  • 19.
    Advantages  Linked listsare more complex to code and manage than arrays, but they have some distinct advantages. a)A linked list can easily grow and shrink in size - ◦ The programmer doesn’t need to know how many nodes will be in the list. They are created in memory as needed. b)Speed of insertion or deletion from the list - ◦ Inserting and deleting elements into and out of arrays requires moving elements of the array. When a node is inserted, or deleted from a linked list, none of the other nodes have to be moved.
  • 20.
    Basic Operations ofa Linked List  As an abstract data type, A list is a finite sequence (possibly empty) of elements with basic operations that vary from one application to another. Basic operations commonly include:  Construction: Allocate and initialize a list object (usually empty)  Empty: Check if list is empty
  • 21.
    Basic Operations ofa Linked List  Traverse: Visit each element of the list in order stored  Insert: Add an item to the list (at any point)  Delete: Remove an item from the list (at any point)
  • 22.
    Basic Operations ofa Linked List  Construction: first = null_value;  Empty: return (first == null_value)  Traverse: ptr = first; while (ptr != null_value) { Process data part of node pointed to by ptr; ptr = address of next element // contained in next part of // node pointed to by ptr; }
  • 23.
    Basic Operations ofa Linked List  Insert & Delete: Both operations necessitate the changing of links. The address of the predecessor of the node to be added or removed needs to be known.
  • 24.
    Outline  Introduction  InsertionDescription  Deletion Description  Basic Node Implementation  Conclusion
  • 25.
    Insertion Description  Insertionat the top of the list  Insertion at the end of the list  Insertion in the middle of the list
  • 26.
    Insertion Description  Insertionat the top of the list  Insertion at the end of the list  Insertion in the middle (any) of the list
  • 27.
    Insertion at thetop Steps:  Create a Node  Set the node data Values  Connect the pointers
  • 28.
    Insertion Description  Followthe previous steps and we get 48 17 142 head // head 93 Step 1 Step 2 Step 3
  • 29.
    Insertion Description  Insertionat the top of the list  Insertion at the end of the list  Insertion in the middle of the list
  • 30.
    Insertion at theend Steps:  Create a Node  Set the node data Values  Connect the pointers
  • 31.
    Insertion Description  Followthe previous steps and we get 48 17 142 head // Step 1 Step 2 Step 3
  • 32.
    Insertion Description  Insertionat the top of the list  Insertion at the end of the list  Insertion in the middle of the list
  • 33.
    Insertion in themiddle Steps:  Create a Node  Set the node data Values  Break pointer connection  Re-connect the pointers
  • 34.
    Insertion Description Step 1Step 2 Step 3 Step 4
  • 35.
    Outline  Introduction  InsertionDescription  Deletion Description  Basic Node Implementation  Conclusion
  • 36.
    Deletion Description  Deletingfrom the top of the list  Deleting from the end of the list  Deleting from the middle of the list
  • 37.
    Deletion Description  Deletingfrom the top of the list  Deleting from the end of the list  Deleting from the middle of the list
  • 38.
    Deleting from thetop Steps  Break the pointer connection  Re-connect the nodes  Delete the node
  • 39.
    Deletion Description 4 17 head 42 6 417 head 42 6 4 17 head 42
  • 40.
    Deletion Description  Deletingfrom the top of the list  Deleting from the end of the list  Deleting from the middle of the list
  • 41.
    Deleting from theend Steps  Break the pointer connection  Set previous node pointer to NULL  Delete the node
  • 42.
    Deletion Description 4 17 head 42 6 417 head 42 6 4 17 6 head
  • 43.
    Deletion Description  Deletingfrom the top of the list  Deleting from the end of the list  Deleting from the middle of the list
  • 44.
    Deleting from theMiddle Steps  Set previous Node pointer to next node  Break Node pointer connection  Delete the node
  • 45.
    Deletion Description 4 1742 head 4 17 head 42 4 head 42
  • 46.
    Outline  Introduction  InsertionDescription  Deletion Description  Basic Node Implementation  Conclusion
  • 47.
    Basic Node Implementation Thefollowing code is written in C++: Struct Node { int data; //any type of data could be another struct Node *next; //this is an important piece of code “pointer” };
  • 48.
    Outline  Introduction  InsertionDescription  Deletion Description  Basic Node Implementation  Conclusion
  • 49.
    Adding a Node Thereare four steps to add a node to a linked list:  Allocate memory for the new node.  Determine the insertion point (you need to know only the new node’s predecessor (pPre)  Point the new node to its successor.  Point the predecessor to the new node.
  • 50.
    Pointer to thepredecessor (pPre) can be in one of two states:  it can contain the address of a node (i.e. you are adding somewhere after the first node – in the middle or at the end)  it can be NULL (i.e. you are adding either to an empty list or at the beginning of the list)
  • 51.
    Adding Nodes toan Empty Linked List pNew -> next = pHead; // set link to NULL pHead = pNew;// point list to first node Before: After: Page 51 51 39 pNew pHead pPre 39 pNew pHead pPre
  • 52.
    Adding a Nodeto the Beginning of a Linked List 39 pNew pHead pPre 75 124 39 pNew pHead pPre 75 124 pNew -> next = pHead; pHead = pNew;// point list to first node
  • 53.
    Adding a Nodeto the End of a Linked List Before: pNew -> next = NULL; pPre ->next = pNew; After: 144 pNew pPre 55 24 144 pNew pPre 55 24
  • 54.
    Adding a Nodeto the Middle of a Linked List 64 pNew pPre 55 124 Before: pNew -> next = pPre -> next; pPre -> next = pNew; After: 64 pNew 55 124 pPre
  • 55.
    Deleting a Nodefrom a Linked List  Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap).  Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL.
  • 56.
     To logicallydelete a node:  First locate the node itself (pCur) and its logical predecessor (pPre).  Change the predecessor’s link field to point to the deleted node’s successor (located at pCur -> next).  Recycle the node
  • 57.
    Deleting the FirstNode from a Linked List Before: pHead = pCur -> next; delete(pCur); After: Page 57 pHead pPre 75 124 pCur pHead pPre Recycle d 124 pCur
  • 58.
    Deleting a Nodefrom a Linked List – General Case  Before:  After: 75 124 96 pPre pCur 75 124 Recycle d pPre pCur
  • 59.
    Code: pPre -> next= pCur -> next; delete(pCur);
  • 60.
    Doubly Link List It contains the data value it contains two pointer  Next, Prev
  • 61.
    Insert at RearEnd (Last)
  • 62.
  • 63.
    Insert at rear newnode->prev=tail tail->next=newnode tail=Newnode
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
    Insert at middle(some point)  newnode->prev=current  newnode->next=current->next  current->next->prev=newnode  current->next=newnode
  • 69.
    Delete at rearend (last)
  • 70.
  • 71.
  • 72.
     lNode* delNode=tail tail=delNode->prev  head=null // (tail==null)  delete delNode
  • 73.
    Delete from Front lNode* delNode=head  head= delnode->next  head->prev=null  delete delnode
  • 74.
    Delete From Middle(Any)  curr->prev->next=curr->next  curr->next-prev=curr->prev  delete curr
  • 75.
    Reading Material CS Schaum’s Outlines: Chapter # 5  D. S. Malik: Chapter # 5  Mark A. Weiss: Chapter # 3  Chapter 6, ADT, Data Structure and Problem solving with C++ by Larry Nyhoff.  Chapter 5, Nell Dale 3rd Edition  Chapter 8, C++ an introduction to Data Structures by Larry Nyhoff