C3
Linked Lists
- Part 1
Learning Objectives
This module aims to provide students with understanding on:
Singly Linked List implementation
Doubly Linked List implementation
Introduction
Arrays are useful in many applications but suffer from two significant limitations:
The size of the array must be known at the time the code is compiled
The elements of the array are the same distance apart in memory, requiring
potentially extensive shifting when inserting a new element
This can be overcome by using linked lists
What is Linked List?
It is a non-sequential collections of independent memory locations (nodes) that store
data and links to other nodes
For every data item in a linked list, there is an associated pointer that would give the
memory location of the next data item in the linked list.
The data items in the linked list are not in consecutive memory locations. They may
be anywhere, but the accessing of these data items is easier as each data item
contains the address of the next data item.
Advantages of Linked List
1. Linked lists are dynamic data structures.
2. Linked lists have efficient memory utilization. Here, memory is not pre-
allocated. Memory is allocated whenever it is required and it is de-allocated
(removed) when it is no longer needed.
3. Insertion and Deletions are easier and efficient. Linked lists provide flexibility in
inserting a data item at a specified position and deletion of the data item from
the given position.
Limitation of Linked List
1. It consumes more space because every node requires an additional pointer to
store address of the next node.
2. Searching a particular element in list is difficult and also time consuming.
Operation Comparison
Comparison of operations between static array and linked list
Operation Static Array Linked List
Linear access efficient efficient
Random access efficient inefficient
Insertion/Deletion inefficient efficient
Element Re-arrangement inefficient efficient
Overhead per element none 1 or 2 links
Types of Linked List
Basically, linked list can be categorized into four types:
1. Singly Linked List.
2. Doubly Linked List.
3. Circular Linked List (remark: not covered in this syllabus)
4. Circular Double Linked List (remark: not covered in this syllabus)
Singly Linked List
Singly Linked List
A singly linked list is one in which all nodes are linked together using a single link
(pointer) in sequential order (forward). It is also known as linear linked list.
head pointer holds address of first node which means the beginning of linked list.
first node’s pointer holds address of second node, ... and so on.
last node in the linked list has its next field set to NULL to mark the end of the list.
Note:
tail pointer is not
compulsory
Basic Operations of Singly Linked List
Node
Insertion
creation
Deletion Traversal
Node Creation
Creating a singly linked list starts with creating a node. Sufficient memory has
to be allocated for creating a node.
Format of a node: Codes: struct datatype{ class className{
Data1 OR Data1
Data2 Data2
Data1 Data2| Pointer Pointer Pointer
}; };
Node Creation and Data Initialization: // Node creation:
class Node {
Node *n = new Node();
public:
int no;
// Data Initialization:
Node *next;
n->no = 6;
};
n->next = NULL;
Exercise 1
Question:
Referring to figure below, write C++ codes for node declaration and creation.
n 2008 Solution:
Struct student{
Name ID next string name;
Ali 103 NULL int id;
student *next;
2008 }
Int main(){
}
Node Insertion
There are three (4) ways to perform node insertion on linked list:
empty linked list beginning of linked list
middle of linked list
end of linked list
Node Insertion (Empty Linked List)
Steps to insert first node into an empty linked list are illustrated as follows:
Step 1 Node creation:
Node *n = new Node();
n->no = 5;
n->next = NULL;
Step 2
Insert first node to linked list:
if (head == NULL)
head = n;
Node Insertion (Empty Linked List)
Steps to insert first node into an empty linked list (with tail pointer) are illustrated as
follows:
Step 1 Node creation:
Node *n = new Node();
n->no = 5;
n->next = NULL;
Step 2
Insert first node to linked list:
if (head == NULL)
head = tail = n;
Node Insertion (End of linked list)
Steps to insert new node into at the end of linked list:
Step 1 Node creation:
Node *n = new Node();
n->no = 6;
n->next = NULL;
Insert new node to end of linked
Step 2 list:
Node *p = head;
Note: while (p->next!=NULL)
Equivalent to p = p->next;
push_back() in both STL
vector and STL list p->next = n;
Node Insertion (End of linked list)
Steps to insert new node at the end of linked list (with tail pointer):
Step 1 Node creation:
Node *n = new Node();
n->no = 6;
n->next = NULL;
Insert new node to end of linked
Step 2 list:
tail->next = n;
tail = n;
Node Insertion (Beginning of linked list)
Steps to insert new node at the beginning of linked list :
Step 1
Node creation:
Node *n = new Node();
n->no = 18;
160
n->next = NULL;
Insert new node to beginning of
Step 2
linked list:
n->next = head;
Note: head = n;
Equivalent to
push_front() in STL list 160
Node Insertion (Middle of linked list)
Steps to insert new node into at the middle of linked list (in between nodes):
Node creation:
Step 1 Node *n = new Node();
n->no = 18;
160 n->next = NULL;
Create additional pointer p and move it
to 1 position before the insertion
Step 2
location:
Node *p = head; int i = 1;
160
while (i<3) {
p = p->next;
i++;
}
Node Insertion (Middle of linked list)
Step 3
Link new node to the node after node
160 pointed by pointer p:
n->next = p->next;
Link node pointed by pointer p to the
Step 4 new node :
p->next = n;
160
Exercise 2
Question:
By assuming a linked list contains more than n nodes where n>100. Insert a new node with value 5, after
a node that contains value 7.
Info next
5
head
Info next Info next Info next Info next Info next
2 8 7 9 9 N
Solution:
Node Deletion
There are three (4) ways to perform node deletion on linked list:
single node beginning of linked list
middle of linked list end of linked list
Node Deletion (Beginning of Linked List)
Steps to delete node at the beginning of linked list are illustrated as follows:
Create a temporary pointer:
head n head Step 1
Node *n = head;
Info next Info next Info next Info next
6 X 2 8 7 N
Step 2
Move head to the next node:
head = head->next;
Question: Delete the targeted node:
Step 3
What if the linked list contains only single node? free(n);
Answer:
The implementation remains same.
Node Deletion (Beginning of Linked List)
Steps to delete node at the beginning of linked list with tail pointer are illustrated as
follows:
Create a temporary pointer:
head n head Step 1
tail
Node *n = head;
Info next Info next Info next Info next
6 X 2 8 7 N
Step 2
Move head to the next node:
head = head->next;
Question:
What if the linked list contains only single node? Delete the targeted node:
Step 3
Answer: free(n);
Additional steps below are required:
if(head == NULL)
tail = NULL;
Node Deletion (Middle of Linked List)
Steps to delete node at the middle of linked list are illustrated as follows:
head n n x tail Step 1
Info next Info next Info next Info next Info next Create a temporary pointer:
9 2 8 6 X 7 N Node *n = head, x;
Step 2 Step 3 Step 4
Move n to 1 position before targeted node: Link node pointed by n to the node Delete the targeted node:
while (n -> next -> Info != 6) after node pointed by x: free(x);
n = n -> next;
n->next = x->next;
Place x at the targeted node: if(x == tail)//if tail used
x = n->next;
tail = n;
Node Deletion (End of Linked List)
Steps to delete node at the end of linked list are illustrated as follows:
head n n x Step 1
Info next Info next Info next Info next Info next Create a temporary pointer:
9 2 8 6 NULL 7 X N
Node *n = head, *x;
Step 2 Step 3 Step 4
Move n to 1 position before last node: Delete the targeted node:
Set next pointer of n node to
while (n -> next -> next != NULL)
free(x);
n = n -> next; NULL:
n->next = NULL;
Place x at the targeted node:
x = n->next;
Node Deletion (End of Linked List)
Steps to delete node at the end of linked list with tail pointer are illustrated as follows:
head n n tail Step 1
Info next Info next Info next Info next Info next Create a temporary pointer:
9 2 8 6 NULL 7 X N
Node *n = head, *x;
Step 2 Step 3 Step 4
Move n to 1 position before node pointed Place x to tail & move pointer Delete the targeted node:
by tail: tail to n: free(x);
while (n -> next != tail)
x = tail;
n = n -> next;
tail = n; Set the next address of tail to NULL:
tail->next = NULL;
Exercise 3
Question:
Provide a C++ code segment to remove the last node.
head
Info next Info next Info next Info next Info next
2 8 7 9 9 N
Node Traversal
Steps to display data in linked list:
head n n n n n
Output:
Info next Info next Info next Info next 9 2 8 7
9 2 8 7 N
Step 1 Step 2
Create a temporary pointer: Perform the operation until pointer n reaches NULL position:
Node *n = head; while (n!= NULL){
cout<<n->Info;
n = n -> next;
}
Exercise
Question:
Provide a C++ code segment to update a node that contains value 7 with value 3.
head tail
Info next Info next Info next Info next Info next
2 8 7 9 9 N
f o r n o w ..
h a t’s a l l
End of Part 1 T x t c l a s s !
y o u i n ne
See