2-LinkedList_YMK
2-LinkedList_YMK
Second Year
(Computer Science and Computer
Technology)
Prepared by
Daw Yee Mon Kyaw
6/10/2024 UCS(Mgy) 1
Basic Array Operations are:
6/10/2024 UCS(Mgy) 2
Linked Lists
▪ Linked list is a linear collection of data elements as an array.
▪ But dynamic data storage which allocates when it is needed.
▪ In a linked list, one element of data storage location is called a
node.
▪ Each node is divided into two parts: the first part contains the
information of the element and the second part contains the
pointer that stores the address of the next node in the list.
6/10/2024 UCS(Mgy) 3
Linked Lists
• Linked lists can be thought of from a high-level perspective
as being a series of nodes.
• Each node has at least a single pointer to the next node and
in the last node’s case a null pointer representing that there
are no more nodes in the linked list.
6/10/2024 UCS(Mgy) 4
Single Linked Lists
• Each node that makes up a singly linked list consists of a
value and a reference to the next node (if any) in the list.
• Linked List Operations:
– Insertion (Insertion is constant operation O(1) )
– Deletion (Deletion is linear operation O(n) )
– Searching (Searching is linear operation O(n) )
– Traversal (Traversing is linear operation O(n) )
6/10/2024 UCS(Mgy) 5
Insertion Operation (Single Linked Lists)
1. head=null in which case the Algorithm Add(value)
node we are adding in now Pre: value is the value to add to the list
both the head and tail of the Post: value has been placed at the tail
tail; or
n ← head
if n.Value = value
if head = tail
// case 2
head ← Ø
tail ← Ø
else
// case 3
head ← head.Next
end if
return true
6/10/2024 UCS(Mgy) 8
end if
while n.Next ≠ Ø and n.Next.Value = value
n ← n.Next
end while
if n.Next ≠ Ø
if n.Next = tail
//case 4
tail ← n
end if
// case 6
return false
end Remove
6/10/2024 UCS(Mgy) 9
6/10/2024 UCS(Mgy) 10
Searching Operation (Single Linked Lists)
Searching a linked list is Algorithm Contains (head, value)
straightforward: we simply Pre: head is the head node in the list
traverse the list checking the value is the value to search for
Post: the item is either in the linked list, true;
value we are looking for with
otherwise false
the value of each node in the n ← head
linked list. while n ≠ Ø and n.Value ≠ value
n ← n.Next
end while
if n ≠ Ø
return false
end if
return true
end Contains
University of Computer Studies, FCS 11
Traversing the list (Single Linked Lists)
You start at the head of the list Algorithm Traverse (head)
and continue until you come Pre: head is the head node in the list
across a node that is Ø. The Post: the item in the list have been
two cases are as follows: traversed
1. node = Ø, we have n ← head
exhausted all nodes in the while n ≠ Ø
linked list; or yield n. Value
2. we must update the node n ← n.Next
reference to be node.Next. end while
end Traverse
6/10/2024 UCS(Mgy) 12
Traversing the list in Reverse Order (Single
Linked Lists)
Algorithm ReverseTraversal (head, tail)
Traversing a singly linked
Pre: head and tail belong to the same list
list in a forward manner (i.e. Post: the items in the list have been
traversed in reverse order
left to right) is simple.
if tail ≠ Ø
We will need to acquire a curr ← tail
while curr ≠ head
reference to the predecessor
prev ← head
of a node, even though the while prev.Next ≠ curr
fundamental characteristics of prev ← prev.Next
end while
the nodes that make up a yield curr.Value
singly linked list make this an curr ← prev
end while
expensive operation.
yield curr.Value
end if
6/10/2024 end ReverseTraversal
UCS(Mgy) 13
Doubly Linked Lists
• Doubly linked lists are very similar to singly linked lists. The only
difference is that each node has a reference to both the next and
previous nodes in the list.
• Doubly Linked List Operations:
– Insertion (Insertion is constant operation O(1) )
– Deletion (Deletion is linear operation O(n) )
– Searching (Searching is linear operation O(n) )
– Traversal (Traversing is linear operation O(n) )
6/10/2024 UCS(Mgy) 14
Insert Operation (Doubly Linked Lists)
Algorithm Add(value)
Pre: value is the value to add to the list
Post: value has been placed at the tail of the list
n ← node(value)
if head=Ø
head ← n
tail ← n
else
n.Previous ← tail
tail.Next ← n
tail ← n
end if
end Add
6/10/2024 UCS(Mgy) 15
Deletion Operation (Doubly Linked Lists)
Deletion of anode
6/10/2024 UCS(Mgy) 17
Reverse Traversal of doubly linked list
6/10/2024 UCS(Mgy) 18