0% found this document useful (0 votes)
5 views

2-LinkedList_YMK

The document provides an overview of linked lists, including basic operations such as traversal, insertion, deletion, searching, and sorting. It distinguishes between singly and doubly linked lists, detailing their structures and the algorithms for various operations. Additionally, it outlines the complexities associated with these operations in both types of linked lists.

Uploaded by

hnryxb4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2-LinkedList_YMK

The document provides an overview of linked lists, including basic operations such as traversal, insertion, deletion, searching, and sorting. It distinguishes between singly and doubly linked lists, detailing their structures and the algorithms for various operations. Additionally, it outlines the complexities associated with these operations in both types of linked lists.

Uploaded by

hnryxb4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Linked List

Second Year
(Computer Science and Computer
Technology)

Prepared by
Daw Yee Mon Kyaw

6/10/2024 UCS(Mgy) 1
Basic Array Operations are:

1. Traversal : Processing each element in the list.


2. Insertion : Adding a new element to the list.
3. Deletion : Removing an element from the list.
4. Searching : Finding the location of the element with a given
value or the record with a given key.
5. Sorting : Arranging the elements in some type of order.
E.g., ascending or descending

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

list; or of the list


n ← node(value)
if head=Ø
2. We simply need to append our
head ← n
node onto the end of the list
tail ← n
updating the tail reference
else
appropriately.
tail.Next ← n
tail ← n
end if
end Add

University of Computer Studies, FCS 6


Deletion Operation (Single Linked Lists)
Deletion of a node from a linked list is straightforward but there
are a few cases we need to account for
1. the list is empty; or

2. the node to remove is the only node in the linked list; or

3. we are removing the head node; or

4. we are removing the tail node; or

5. the node to remove is somewhere in between the head and

tail; or

6. the item to remove doesn’t exist in the linked list

University of Computer Studies, FCS 7


Algorithm Remove(head, value)
Pre: head is the head node in the list
value is the value to remove from the list
Post: value is removed from the list, true; otherwise false
if head = Ø
// case 1
return false
end if

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

// this is only case 5 if the condition on line 25 was false


n.Next ← n.Next.Next
return true
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)

Algorithm Remove(head, value) while n ≠ Ø and value ≠ n.Value


Pre: head is the head node in the list n ← n.Next
value is the value to remove from the list end while
Post: value is removed from the list, true; otherwise if n = tail
false tail ← tail.Previous
if head = Ø tail.Next ← Ø
return false return true
end if else if n ≠ Ø
if value= head.Value n.Previous.Next ← n.Next
if head = tail n.Next.Previous ← n.Previous
head ← Ø return true
tail ← Ø end if
else return false
head ← head.Next end Remove
head.Previous ← Ø Deletion of a node
end if
return true
end if
n ← head.Next

University of Computer Studies, FCS 16


Deletion Operation (Doubly Linked Lists)

Deletion of anode

6/10/2024 UCS(Mgy) 17
Reverse Traversal of doubly linked list

Algorithm ReverseTraversal (tail)


Pre: tail is the tail node of the list to traverse
Post: the list has been traversed in reverse order
n ← tail
while n ≠ Ø
yield n.Value
n ← n.Previous
end while
end ReverseTraversal

6/10/2024 UCS(Mgy) 18

You might also like